我应该在C#中使用哪些类来获取有关我网络中某台计算机的信息?(比如谁登录该计算机,该计算机上运行的操作系统,打开的端口等)
Checkout System.Management和System.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); }
注意:必须定义userID,password和ipAddress以匹配您的环境.