1、Windows Management Instrumentation(WMI)提供了获取信息的方法,在C#中可用通过System.Management命名空间中的类访问。比如获取CPU ID的方法:
stringGetCpuID()
{try{stringid="";
ManagementClass mc=newManagementClass("Win32_Processor");
ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc)
{
id=mo.Properties["ProcessorId"].Value.ToString();
}returnid;
}catch{return"unknow";
}
}
2、那么像“Win32_Processor”、“ProcessorId”一类的字符串该如何写?这里有详细说明:
http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx
比如主板信息的Win32_BaseBoard类:
classWin32_BaseBoard : CIM_Card
{stringCaption;stringConfigOptions[];stringCreationClassName;
real32 Depth;stringDescription;
real32 Height;
boolean HostingBoard;
boolean HotSwappable;
datetime InstallDate;stringManufacturer;stringModel;stringName;stringOtherIdentifyingInfo;stringPartNumber;
boolean PoweredOn;stringProduct;
boolean Removable;
boolean Replaceable;stringRequirementsDescription;
boolean RequiresDaughterBoard;stringSerialNumber;stringSKU;stringSlotLayout;
boolean SpecialRequirements;stringStatus;stringTag;stringVersion;
real32 Weight;
real32 Width;
};
如果我们想知道主板的SerialNumber,参照Win32_BaseBoard类的红色文字,我们就可以写出如下代码:
stringGetMotherBoardSerialNumber()
{try{stringsn="";
ManagementClass mc=newManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc)
{
sn=mo.Properties["SerialNumber"].Value.ToString();
}returnsn;
}catch{return"unknow";
}
}
3、例子:
注释部分是在测试机器上得到的值。
privatevoidCpuInfo()
{try{
ManagementClass mc=newManagementClass("Win32_Processor");
ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc)
{
UInt16 AddressWidth=(UInt16)(mo.Properties["AddressWidth"].Value);//32UInt16 DataWidth=(UInt16)(mo.Properties["DataWidth"].Value);//32UInt32 CurrentClockSpeed=(UInt32)(mo.Properties["CurrentClockSpeed"].Value);//2810UInt32 MaxClockSpeed=(UInt32)(mo.Properties["MaxClockSpeed"].Value);//2810UInt32 ExtClock=(UInt32)(mo.Properties["ExtClock"].Value);//200stringManufacturer=mo.Properties["Manufacturer"].Value.ToString();//GenuineIntelstringCaption=mo.Properties["Caption"].Value.ToString();//x86 Family 15 Model 4 Stepping 7stringName=mo.Properties["Name"].Value.ToString();//Intel(R) Pentium(R) D CPU 2.80GHzstringSocketDesignation=mo.Properties["SocketDesignation"].Value.ToString();//socket775}
}catch(Exception ex)
{
Console.WriteLine("unknow");
}
}
privatevoidGetMotherBoardInfo()
{try{
ManagementClass mc=newManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc)
{stringManufacturer=mo.Properties["Manufacturer"].Value.ToString();//ASUSTeK Computer INC.stringName=mo.Properties["Name"].Value.ToString();//底板stringProduct=mo.Properties["Product"].Value.ToString();//P5PL2stringSlotLayout=mo.Properties["SlotLayout"].Value.ToString();//在测试机器上没有得到该信息stringVersion=mo.Properties["Version"].Value.ToString();//Rev 1.xx}
}catch(Exception ex)
{
Console.WriteLine("unknow");
}
}
privatevoidDiskInfo()
{try{
ManagementClass mc=newManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc)
{stringInterfaceType=mo.Properties["InterfaceType"].Value.ToString();//IDEstringModel=mo.Properties["Model"].Value.ToString();//MAXTOR 6L080J4stringName=mo.Properties["Name"].Value.ToString();//\\.\PHYSICALDRIVE0UInt32 Partitions=(UInt32)(mo.Properties["Partitions"].Value);//1UInt64 Size=(UInt64)(mo.Properties["Size"].Value);//80048424960UInt64 TotalCylinders=(UInt64)(mo.Properties["TotalCylinders"].Value);//9732UInt32 TotalHeads=(UInt32)(mo.Properties["TotalHeads"].Value);//255UInt64 TotalSectors=(UInt64)(mo.Properties["TotalSectors"].Value);//156344580UInt64 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
来源:https://www.cnblogs.com/h2appy/archive/2008/09/03/1282792.html