为了获取网络中某台计算机的信息,我应该在 C# 中使用哪些类? (例如谁登录了该计算机、该计算机上正在运行什么操作系统、打开了哪些端口等)
查看系统管理 http://msdn.microsoft.com/en-us/library/system.management(loband).aspx and 系统.管理.管理类 http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx。两者都用于访问 WMI,即如何获取相关信息。
Edit:更新了示例以从远程计算机访问 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 地址以匹配您的环境。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)