C#:获取有关域中计算机的信息

问题描述:

我应该在C#中使用哪些类以便获取有关网络中某台计算机的信息? (例如谁在该计算机上登录,在该计算机上运行什么操作系统,打开哪些端口等)C#:获取有关域中计算机的信息

+1

您是否试图在目标机器上运行此代码?或者您是否试图通过计算机运行此程序,并通过网络获取有关另一台计算机的信息? – Nate 2009-06-09 17:26:16

+0

我想从我的计算机上运行代码,以便通过网络获取有关另一台计算机的信息,了解其IP。 – melculetz 2009-06-09 18:51:49

+0

在我的答案中提供了远程WMI查询的示例。 – Nate 2009-06-09 20:52:00

WMI Library,这里是一个VB.net example。它不应该很难转换为C#

查看WMI库。

结账System.ManagementSystem.Management.ManagementClass。两者都用于访问WMI,这是如何获取有问题的信息。

编辑:更新了样品从远程计算机访问WMI:

ConnectionOptions options; 
options = new ConnectionOptions(); 

options.Username = userID; 
options.Password = password; 
options.EnablePrivileges = true; 
options.Impersonation = ImpersonationLevel.Impersonate; 

ManagementScope scope; 
scope = new ManagementScope("\\\\" + ipAddress + "\\root\\cimv2", options); 
scope.Connect(); 

String queryString = "SELECT PercentProcessorTime, PercentInterruptTime, InterruptsPersec FROM Win32_PerfFormattedData_PerfOS_Processor"; 

ObjectQuery query; 
query = new ObjectQuery(queryString); 

ManagementObjectSearcher objOS = new ManagementObjectSearcher(scope, query); 

DataTable dt = new DataTable(); 
dt.Columns.Add("PercentProcessorTime"); 
dt.Columns.Add("PercentInterruptTime"); 
dt.Columns.Add("InterruptsPersec"); 

foreach (ManagementObject MO in objOS.Get()) 
{ 
    DataRow dr = dt.NewRow(); 
    dr["PercentProcessorTime"] = MO["PercentProcessorTime"]; 
    dr["PercentInterruptTime"] = MO["PercentInterruptTime"]; 
    dr["InterruptsPersec"] = MO["InterruptsPersec"]; 

    dt.Rows.Add(dr); 
} 

注:用户ID,密码和ip地址都必须被定义为匹配您的环境。

这里是一个使用它像在一个约盒中的例子。 MSDN拥有其余所有项目。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Management; 

namespace About_box 
{ 
    public partial class About : Form 
    { 
     public About() 
     { 
      InitializeComponent(); 
      FormLoad(); 
     } 

     public void FormLoad() 
     { 
      SystemInfo si; 
      SystemInfo.GetSystemInfo(out si); 

      txtboxApplication.Text = si.AppName; 
      txtboxVersion.Text = si.AppVersion; 
      txtBoxComputerName.Text = si.MachineName; 
      txtBoxMemory.Text = Convert.ToString((si.TotalRam/1073741824) 
       + " GigaBytes"); 
      txtBoxProcessor.Text = si.ProcessorName; 
      txtBoxOperatingSystem.Text = si.OperatingSystem; 
      txtBoxOSVersion.Text = si.OperatingSystemVersion; 
      txtBoxManufacturer.Text = si.Manufacturer; 
      txtBoxModel.Text = si.Model; 
     } 


    } 
}