C#获取电脑硬件信息

获取CPU编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_Processor");  
ManagementObjectCollection moc = mc.GetInstances();  
string strID = null ;    
foreach( ManagementObject mo in moc )    
   
 strID = mo.Properties["ProcessorId"].Value.ToString();  
 break;   
             
textBox1.Text +=  "CPU ID:" + strID; 
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            string strID = null ; 
            foreach( ManagementObject mo in moc ) 
           
             strID = mo.Properties["ProcessorId"].Value.ToString();
             break;
                     
            textBox1.Text +=  "CPU ID:" + strID;

     返回结果:

        电脑1:CPU ID:BFEBFBFF00000F27

        电脑2:CPU ID:BFEBFBFF00000F27

        电脑3:CPU ID:BFEBFBFF00000F29

        电脑4:CPU ID:BFEBFBFF00000F29

获取主板编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_BaseBoard");  
ManagementObjectCollection moc = mc.GetInstances();  
string strID = null ;    
foreach( ManagementObject mo in moc )    
   
 strID = mo.Properties["SerialNumber"].Value.ToString();  
 break;   
             
textBox1.Text +=  "主板 ID:" + strID; 
            ManagementClass mc = new ManagementClass("Win32_BaseBoard");
            ManagementObjectCollection moc = mc.GetInstances();
            string strID = null ; 
            foreach( ManagementObject mo in moc ) 
           
             strID = mo.Properties["SerialNumber"].Value.ToString();
             break;
                     
            textBox1.Text +=  "主板 ID:" + strID;


      返回结果:

        电脑1:主板 ID:

        电脑2:主板 ID:CN24401483

        电脑3:主板 ID:AZF241001101

        电脑4:主板 ID:


 

获取硬盘编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");  
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。  
ManagementObjectCollection moc = mc.GetInstances();  
string strID = null ;    
foreach( ManagementObject mo in moc )    
   
 strID = mo.Properties["SerialNumber"].Value.ToString();  
 break;   
             
textBox1.Text +=  "硬盘 ID:" + strID; 
            ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
            //网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
            ManagementObjectCollection moc = mc.GetInstances();
            string strID = null ; 
            foreach( ManagementObject mo in moc ) 
           
             strID = mo.Properties["SerialNumber"].Value.ToString();
             break;
                     
            textBox1.Text +=  "硬盘 ID:" + strID;


      返回结果:

        电脑1:硬盘 ID:4833395344463658202020202020202020202020

        电脑2:硬盘 ID:WD-WMAJD1092385

        电脑3:硬盘 ID:4a353756354d5939202020202020202020202020

        电脑4:硬盘 ID:0637J2FW508014


获取BIOS编号:
view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_BIOS");  
ManagementObjectCollection moc = mc.GetInstances();  
string strID = null ;    
foreach( ManagementObject mo in moc )    
   
 strID = mo.Properties["SerialNumber"].Value.ToString();  
 break;   
             
textBox1.Text +=  "BIOS ID:" + strID; 
            ManagementClass mc = new ManagementClass("Win32_BIOS");
            ManagementObjectCollection moc = mc.GetInstances();
            string strID = null ; 
            foreach( ManagementObject mo in moc ) 
           
             strID = mo.Properties["SerialNumber"].Value.ToString();
             break;
                     
            textBox1.Text +=  "BIOS ID:" + strID;


     返回结果:

        电脑1:BIOS ID: 

        电脑2:BIOS ID:CN24401483

        电脑3:BIOS ID:

        电脑4:BIOS ID:

 


    总结:

    由以上各步看出,通过Win32_Processor获取CPUID不正确,或者说Win32_Processor字段就不包含CPU编号信息。

    通过Win32_BaseBoard获取主板信息,但不是所有的主板都有编号,或者说不是能获取所有系统主板的编号。

    通过Win32_PhysicalMedia获取硬盘编号应该没有问题。但网上说可以通过Win32_DiskDrive获取,其实所得信息根本不包含SerialNumber。

    通过Win32_BIOS获取BIOS信息,基本和获取主板信息差不多。就是说:不是所有的主板BIOS信息都有编号。


    另外,可以将通过以上各字段所得信息输出,逐个查看所有信息 属性和对应的值。代码如下:

view plaincopy to clipboardprint?
ManagementClass mc = new ManagementClass("Win32_Processor");  
ManagementObjectCollection moc = mc.GetInstances();   
foreach( ManagementObject mo in moc )    
   
 textBox1.Text += "\r\n============CUP信息===========";  
 foreach (PropertyData pd in mo.Properties)  
  
     textBox1.Text += "\r\n" + pd.Name + "\t";  
     if (pd.Value != null)  
     
         textBox1.Text += pd.Value.ToString();  
     
  
 textBox1.Text += "\r\n\r\n=======================";  
 

 

你可能感兴趣的:(C#)