Home  >  Article  >  Backend Development  >  How to obtain computer hardware information through C# programming?

How to obtain computer hardware information through C# programming?

黄舟
黄舟Original
2017-08-07 13:10:221969browse

This article mainly introduces the method of C# programming to obtain various computer hardware information, and analyzes the relevant operating skills and precautions for C# to obtain computer CPU, motherboard, hard disk, BIOS number and other information in the form of examples. Friends who need it can Refer to the following

The example of this article describes the method of obtaining various computer hardware information through C# programming. Share it with everyone for your reference, the details are as follows:

Get the CPU number:


##

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;

Return the result:


电脑1:CPU ID:BFEBFBFF00000F27
电脑2:CPU ID:BFEBFBFF00000F27
电脑3:CPU ID:BFEBFBFF00000F29
电脑4:CPU ID:BFEBFBFF00000F29

Get the motherboard number:


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;

Return the result:


电脑1:主板 ID:
电脑2:主板 ID:CN24401483
电脑3:主板 ID:AZF241001101
电脑4:主板 ID:

Get the hard disk number:


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;

Return the result:


电脑1:硬盘 ID:4833395344463658202020202020202020202020
电脑2:硬盘 ID:WD-WMAJD1092385
电脑3:硬盘 ID:4a353756354d5939202020202020202020202020
电脑4:硬盘 ID:0637J2FW508014

Get the BIOS number:


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;

Return the result:


电脑1:BIOS ID:
电脑2:BIOS ID:CN24401483
电脑3:BIOS ID:
电脑4:BIOS ID:

Summary :

It can be seen from the above steps that the CPUID obtained through Win32_Processor is incorrect, or the Win32_Processor field does not contain the CPU number information.

Obtain motherboard information through Win32_BaseBoard, but not all motherboards have numbers, or it is not possible to obtain the numbers of all system motherboards.

There should be no problem in getting the hard disk number through Win32_PhysicalMedia. However, it is said online that it can be obtained through Win32_DiskDrive. In fact, the information obtained does not include SerialNumber at all.

Obtaining BIOS information through Win32_BIOS is basically the same as obtaining motherboard information. That is to say: not all motherboard BIOS information has a number.

In addition, you can output the information obtained through each of the above fields and view all information attributes and corresponding values ​​one by one. The code is as follows:


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

The above is the detailed content of How to obtain computer hardware information through C# programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn