SoFunction
Updated on 2025-03-07

WMI method to obtain hardware information encapsulation function (Lenovo desktop factory number CPUID BIOS serial number Hard disk information Graphics card information MAC address)

Today I played WMI and checked the hardware information of the computer. I felt that many codes could be extracted, so I put forward the public parts myself. If I want to obtain the hardware information of a certain part, I don’t have to write functions one by one. For example, if I get the MAC address, I write a function to obtain the MAC address, and if I get the CPU information, I write a function to obtain the CPU information. It’s too troublesome.

The following is the function code:

Copy the codeThe code is as follows:

private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            mc = new (wmiClass);
            moc = ();
            foreach ( mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }

                }
            }
            return result;
        }


        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            mc = new (wmiClass);
            moc = ();
            foreach ( mo in moc)
            {
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }

            }
            return result;
        }

Get CPUID

Copy the codeThe code is as follows:

private static string cpuId()
        {    
            string retVal = identifier("Win32_Processor", "UniqueId");  //CPUID  
            retVal += identifier("Win32_Processor", "ProcessorId");
retVal += identifier("Win32_Processor", "Name"); //Processor name
retVal += identifier("Win32_Processor", "Manufacturer"); //Processor Manufacturer
retVal +=identifier("Win32_Processor", "MaxClockSpeed"); //Maximum clock frequency
            return retVal;
        }

Get BIOS information, where the BIOS serial number is the factory number of Lenovo desktop. I see that the automatic access to the host number in Lenovo's warranty page should also call the "SerialNumber of "Win32_BIOS".

Repair page URL: /lenovo/wsi/wsbx/lenovo/#minarepairInfo

Copy the codeThe code is as follows:

//BIOS information
        private static string biosId()
        {
return identifier("Win32_BIOS", "Manufacturer")                                                                                                                     �
                    + identifier("Win32_BIOS", "SMBIOSBIOSVersion")  //
                    + identifier("Win32_BIOS", "IdentificationCode") //
+ identifier("Win32_BIOS", "SerialNumber")        //BIOS serial number
+ identifier("Win32_BIOS", "ReleaseDate")                                                                                                                     �
+ identifier("Win32_BIOS", "Version");                                                                                                                      �
        }

Get hard disk information:

Copy the codeThe code is as follows:

private static string diskId()
        {
return identifier("Win32_DiskDrive", "Model")
+ identifier("Win32_DiskDrive", "Manufacturer") //Manufacturer
+ identifier("Win32_DiskDrive", "Signature")    //Signature
+ identifier("Win32_DiskDrive", "TotalHeads"); //Sector header
        }

Get graphics card information:

Copy the codeThe code is as follows:

private static string videoId()
         {
            return identifier("Win32_VideoController", "DriverVersion")
                     + identifier("Win32_VideoController", "Name");
        }

Obtain network card MAC address information:

Copy the codeThe code is as follows:

private static string macId()
         {
             return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
        }