WMI 获取硬件信息的封装函数与获取联想台式机的出厂编号方法

今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都是可以提取出来的,就自己把那些公共部分提出出来,以后如果要获取

某部分的硬件信息就不用写一个一个的函数,比如获取MAC地址就写一个获取MAC地址的函数,获取CPU 信息就写一个获取CPU信息的

函数,太麻烦了

如下是函数代码:

 1         private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
 2         {
 3             string result = "";
 4             System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
 5             System.Management.ManagementObjectCollection moc = mc.GetInstances();
 6             foreach (System.Management.ManagementObject mo in moc)
 7             {
 8                 if (mo[wmiMustBeTrue].ToString() == "True")
 9                 {
10                     if (result == "")
11                     {
12                         try
13                         {
14                             result = mo[wmiProperty].ToString();
15                             break;
16                         }
17                         catch
18                         {
19                         }
20                     }
21 
22                 }
23             }
24             return result;
25         }
26 
27 
28         private static string identifier(string wmiClass, string wmiProperty)
29         {
30             string result = "";
31             System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
32             System.Management.ManagementObjectCollection moc = mc.GetInstances();
33             foreach (System.Management.ManagementObject mo in moc)
34             {
35                 if (result == "")
36                 {
37                     try
38                     {
39                         result = mo[wmiProperty].ToString();
40                         break;
41                     }
42                     catch
43                     {
44                     }
45                 }
46 
47             }
48             return result;
49         }

获取CPUID

1         private static string cpuId()
2         {     
3             string retVal = identifier("Win32_Processor", "UniqueId");  //CPUID   
4             retVal += identifier("Win32_Processor", "ProcessorId");
5             retVal += identifier("Win32_Processor", "Name");  //处理器名称
6             retVal += identifier("Win32_Processor", "Manufacturer");  //处理器制造商
7             retVal +=identifier("Win32_Processor", "MaxClockSpeed");  //最大时钟频率
8             return retVal;
9         }

 

获取BIOS信息,其中BIOS序列号就是联想台式机的出厂编号,我看联想的保修页面里的自动获取主机编号应该也是调用

这个"Win32_BIOS"的 "SerialNumber

报修页面网址:http://support1.lenovo.com.cn/lenovo/wsi/wsbx/lenovo/#minarepairInfo

 

 1         //BIOS信息
 2         private static string biosId()
 3         {
 4             return identifier("Win32_BIOS", "Manufacturer")          //BIOS制造商名称
 5                     + identifier("Win32_BIOS", "SMBIOSBIOSVersion")  //
 6                     + identifier("Win32_BIOS", "IdentificationCode") //
 7                     + identifier("Win32_BIOS", "SerialNumber")       //BIOS序列号
 8                     + identifier("Win32_BIOS", "ReleaseDate")        //出厂日期
 9                     + identifier("Win32_BIOS", "Version");           //版本号
10         }

 

 

获取硬盘信息:

1         private static string diskId()
2         {
3             return identifier("Win32_DiskDrive", "Model")           //模式
4                     + identifier("Win32_DiskDrive", "Manufacturer") //制造商
5                     + identifier("Win32_DiskDrive", "Signature")    //签名
6                     + identifier("Win32_DiskDrive", "TotalHeads");  //扇区头
7         }

 

获取主板信息:

1         private static string baseId()
2         {
3             return identifier("Win32_BaseBoard", "Model")              
4                     + identifier("Win32_BaseBoard", "Manufacturer")    
5                     + identifier("Win32_BaseBoard", "Name")
6                     + identifier("Win32_BaseBoard", "SerialNumber");
7         }

获取显卡信息:

1         private static string videoId()
2         {
3             return identifier("Win32_VideoController", "DriverVersion")
4                     + identifier("Win32_VideoController", "Name");
5         }

 

获取网卡MAC地址信息:

1         private static string macId()
2         {
3             return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); 
4         }

 

 

如有什么不对的地方,欢迎大家拍砖!!

 

 

 

 

posted @ 2012-04-20 00:20  桦仔  阅读(3198)  评论(0编辑  收藏  举报