C#获取CPU等硬件ID(转载)
System.Management命名空间提供对大量管理信息和管理事件集合的访问,这些信息和事件是与根据 Windows 管理规范 (WMI) 结构对系统、设备和应用程序设置检测点有关的。应用程序和服务可以使用从 ManagementObjectSearcher 和 ManagementQuery 派生的类,查询感兴趣的管理信息(例如在磁盘上还剩多少可用空间、当前 CPU 利用率是多少、某一应用程序正连接到哪一数据库等等);或者应用程序和服务可以使用 ManagementEventWatcher 类预订各种管理事件。这些可访问的数据可以来自分布式环境中托管的和非托管的组件。
首先添加对System.Management类库的引用。然后添加下述代码,就可获得CPU、主板和硬盘等的编号。
1,获取CPU编号
System.Management.ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
MessageBox.Show(mo["processorid"].ToString());
}
2,获取主板ID
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject mo in searcher.Get())
{
MessageBox.Show( mo["SerialNumber"].ToString().Trim());
}
或者:
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mx in moc)
{
MessageBox.Show( mx.Properties["SerialNumber"].Value.ToString());
}
3,获取硬盘ID
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
MessageBox.Show(mo.Properties["Model"].Value.ToString());
}
4,获取网卡地址
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
MessageBox.Show(mo["MacAddress"].ToString());
mo.Dispose();
}
5,获取内存信息
System.Management.ManagementClass mcMemory = new System.Management.ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection mocMemory = mcMemory.GetInstances();
foreach (ManagementObject mo in mocMemory)
{
if (mo.Properties["TotalVisibleMemorySize"].Value != null)
{
MessageBox.Show(mo.Properties["TotalVisibleMemorySize"].Value.ToString());
}
}
来自:http://blog.csdn.net/redder_xu/article/details/6773999