C#:获取有关域中计算机的信息
答
查看WMI库。
答
结账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);
}
注:用户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;
}
}
}
您是否试图在目标机器上运行此代码?或者您是否试图通过计算机运行此程序,并通过网络获取有关另一台计算机的信息? – Nate 2009-06-09 17:26:16
我想从我的计算机上运行代码,以便通过网络获取有关另一台计算机的信息,了解其IP。 – melculetz 2009-06-09 18:51:49
在我的答案中提供了远程WMI查询的示例。 – Nate 2009-06-09 20:52:00