C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号
http://www.cnblogs.com/liufei88866/archive/2010/05/11/1732671.html?
?
最近學習過程中,想到提取系統硬件信息做一些驗證,故而對網上提到的利用.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=======================";??
}???
posted on 2010-08-26 22:03 djlzxzy 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/djlzxzy/archive/2010/08/26/1809532.html
總結
以上是生活随笔為你收集整理的C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Platform Builder 6.0
- 下一篇: MVC --.Routing