获取计算机的信息(IP地址、MAC地址、CUP序列号、硬盘序列号、主板信息等等)...
1、Windows Management Instrumentation(WMI)提供了獲取信息的方法,在C#中可用通過System.Management命名空間中的類訪問。比如獲取CPU ID的方法:
?
????????string?GetCpuID()????????{
????????????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類:
?
class?Win32_BaseBoard?:?CIM_Card{
??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");
????????????}
????????}
?
????????private?void?GetMotherBoardInfo()????????{
????????????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");
????????????}
????????}
?
????????private?void?DiskInfo()????????{
????????????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?
?
?
轉載于:https://www.cnblogs.com/h2appy/archive/2008/09/03/1282792.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的获取计算机的信息(IP地址、MAC地址、CUP序列号、硬盘序列号、主板信息等等)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最短路径问题的算法实现【转载】
- 下一篇: 设置Qt应用程序图标