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

mel*_*etz 7 .net c# network-programming

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

Nat*_*ate 9

Checkout 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);
}
Run Code Online (Sandbox Code Playgroud)

注意:必须定义userID,password和ipAddress以匹配您的环境.


Sho*_*ban 2

WMI 库,这里是一个VB.net 示例。将其转换为 C# 应该不难