SoFunction
Updated on 2025-03-06

Examples of C# programming method to obtain various computer hardware information

This article describes the method of C# programming to obtain various computer hardware information. Share it for your reference, as follows:

Get the CPU number:

ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = ();
string strID = null ;
foreach( ManagementObject mo in moc )
{
  strID = ["ProcessorId"].();
  break;
}
 += "CPU ID:" + strID;

Return result:

computer1:CPU ID:BFEBFBFF00000F27
computer2:CPU ID:BFEBFBFF00000F27
computer3:CPU ID:BFEBFBFF00000F29
computer4:CPU ID:BFEBFBFF00000F29

Get the motherboard number:

ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = ();
string strID = null ;
foreach( ManagementObject mo in moc )
{
  strID = ["SerialNumber"].();
  break;
}
 += "Motherboard ID:" + strID;

Return result:

computer1:Motherboard ID:
computer2:Motherboard ID:CN24401483
computer3:Motherboard ID:AZF241001101
computer4:Motherboard ID:

Get the hard disk number:

ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
//It is mentioned on the Internet that Win32_DiskDrive is used, but the hard disk information obtained with Win32_DiskDrive does not contain the SerialNumber attribute.ManagementObjectCollection moc = ();
string strID = null ;
foreach( ManagementObject mo in moc )
{
  strID = ["SerialNumber"].();
  break;
}
 += "Hard disk ID:" + strID;

Return result:

computer1:harddisk ID:4833395344463658202020202020202020202020
computer2:harddisk ID:WD-WMAJD1092385
computer3:harddisk ID:4a353756354d5939202020202020202020202020
computer4:harddisk ID:0637J2FW508014

Get the BIOS number:

ManagementClass mc = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = ();
string strID = null ;
foreach( ManagementObject mo in moc )
{
  strID = ["SerialNumber"].();
  break;
}
 += "BIOS ID:" + strID;

Return result:

computer1:BIOS ID:
computer2:BIOS ID:CN24401483
computer3:BIOS ID:
computer4:BIOS ID:

Summarize:

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

Get motherboard information through Win32_BaseBoard, but not all motherboards have numbers, or they cannot get the numbers of all system motherboards.

There should be no problem getting the hard disk number through Win32_PhysicalMedia. But it is said online that it can be obtained through Win32_DiskDrive, but the information obtained does not contain 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, the information obtained through the above fields can be output to view all information attributes and corresponding values ​​one by one. The code is as follows:

ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = ();
foreach( ManagementObject mo in moc )
{
  += "\r\n============CUPinformation===========";
 foreach (PropertyData pd in )
 {
    += "\r\n" +  + "\t";
   if ( != null)
   {
      += ();
   }
 }
  += "\r\n\r\n=======================";
}

For more information about C# related content, please check out the topic of this site:Summary of thread usage techniques for C# programming》、《Summary of WinForm control usage》、《Tutorial on the usage of common C# controls》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.