C#擷取電腦硬體資訊(CPU ID、主板ID、硬碟ID、BIOS編

來源:互聯網
上載者:User

最近學習過程中,想到提取系統硬體資訊做一些驗證,故而對網上提到的利用.NET System.Management類擷取硬體資訊做了進一步的學習、驗證。驗證是分別在4台電腦,XP SP3系統中進行,特將驗證過程記錄於此。

    說明:

電腦1(聯想品牌電腦);

電腦2(HP品牌電腦);

電腦3(聯想品牌電腦);

電腦4(相容機);

擷取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=======================";  
}  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.