1、Windows Management Instrumentation(WMI)提供了获取信息的方法,在C#中可用通过System.Management命名空间中的类访问。比如获取CPU ID的方法:
{
try
{
string id = "" ;
ManagementClass mc = new ManagementClass( " Win32_Processor " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
id = mo.Properties[ " ProcessorId " ].Value.ToString();
}
return id;
}
catch
{
return " unknow " ;
}
}
2、那么像“Win32_Processor”、“ProcessorId”一类的字符串该如何写?这里有详细说明:
http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx
比如主板信息的Win32_BaseBoard类:
{
string Caption;
string ConfigOptions[];
string CreationClassName;
real32 Depth;
string Description;
real32 Height;
boolean HostingBoard;
boolean HotSwappable;
datetime InstallDate;
string Manufacturer;
string Model;
string Name;
string OtherIdentifyingInfo;
string PartNumber;
boolean PoweredOn;
string Product;
boolean Removable;
boolean Replaceable;
string RequirementsDescription;
boolean RequiresDaughterBoard;
string SerialNumber;
string SKU;
string SlotLayout;
boolean SpecialRequirements;
string Status;
string Tag;
string Version;
real32 Weight;
real32 Width;
};
如果我们想知道主板的SerialNumber,参照Win32_BaseBoard类的红色文字,我们就可以写出如下代码:
{
try
{
string sn = "" ;
ManagementClass mc = new ManagementClass( " Win32_BaseBoard " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
sn = mo.Properties[ "SerialNumber " ].Value.ToString();
}
return sn;
}
catch
{
return " unknow " ;
}
}
3、例子:
注释部分是在测试机器上得到的值。
{
try
{
ManagementClass mc = new ManagementClass( " Win32_Processor " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
UInt16 AddressWidth = (UInt16)(mo.Properties[ " AddressWidth " ].Value); // 32
UInt16 DataWidth = (UInt16)(mo.Properties[ " DataWidth " ].Value); // 32
UInt32 CurrentClockSpeed = (UInt32)(mo.Properties[ " CurrentClockSpeed " ].Value); // 2810
UInt32 MaxClockSpeed = (UInt32)(mo.Properties[ " MaxClockSpeed " ].Value); // 2810
UInt32 ExtClock = (UInt32)(mo.Properties[ " ExtClock " ].Value); // 200
string Manufacturer = mo.Properties[ " Manufacturer " ].Value.ToString(); // GenuineIntel
string Caption = mo.Properties[ " Caption " ].Value.ToString(); // x86 Family 15 Model 4 Stepping 7
string Name = mo.Properties[ " Name " ].Value.ToString(); // Intel(R) Pentium(R) D CPU 2.80GHz
string SocketDesignation = mo.Properties[ " SocketDesignation " ].Value.ToString(); // socket775
}
}
catch (Exception ex)
{
Console.WriteLine( " unknow " );
}
}
{
try
{
ManagementClass mc = new ManagementClass( " Win32_BaseBoard " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string Manufacturer = mo.Properties[ " Manufacturer " ].Value.ToString(); // ASUSTeK Computer INC.
string Name = mo.Properties[ " Name " ].Value.ToString(); // 底板
string Product = mo.Properties[ " Product " ].Value.ToString(); // P5PL2
string SlotLayout = mo.Properties[ " SlotLayout " ].Value.ToString(); // 在测试机器上没有得到该信息
string Version = mo.Properties[ " Version " ].Value.ToString(); // Rev 1.xx
}
}
catch (Exception ex)
{
Console.WriteLine( " unknow " );
}
}
{
try
{
ManagementClass mc = new ManagementClass( " Win32_DiskDrive " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string InterfaceType = mo.Properties[ " InterfaceType " ].Value.ToString(); // IDE
string Model = mo.Properties[ " Model " ].Value.ToString(); // MAXTOR 6L080J4
string Name = mo.Properties[ " Name " ].Value.ToString(); // \\.\PHYSICALDRIVE0
UInt32 Partitions = (UInt32)(mo.Properties[ " Partitions " ].Value); // 1
UInt64 Size = (UInt64)(mo.Properties[ " Size " ].Value); // 80048424960
UInt64 TotalCylinders = (UInt64)(mo.Properties[ " TotalCylinders " ].Value); // 9732
UInt32 TotalHeads = (UInt32)(mo.Properties[ " TotalHeads " ].Value); // 255
UInt64 TotalSectors = (UInt64)(mo.Properties[ " TotalSectors " ].Value); // 156344580
UInt64 TotalTracks = (UInt64)(mo.Properties[ " TotalTracks " ].Value); // 2481660
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
4、用WMI获得信息也存在一个小问题,就是比较慢,性能不好。如果对性能有要求,就要通过平台调用的方式调用Win32相关函数。比如通过下面函数可得到物理内存大小等信息:
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);
5、其他参考:
http://www.cnblogs.com/draeag/archive/2007/06/21/791992.html