WMI get hardware information package function method of lenovo desktop factory serial number CPUID BIOS hard disk information graphics card information MAC address

  • 2020-05-26 10:00:07
  • OfStack

Today to play 1 WMI, query 1 under the computer hardware information, feeling a lot of code can be extracted, themselves to the public part, later if you want to get a part of the hardware information 1 don't have to write a function, such as obtaining MAC address write 1 get MAC function, CPU information will write a function to the information of CPU, too much trouble

Here is the function code:


private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject 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 = "";
            System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            return result;
        }

Get CPUID


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 the BIOS information, where the BIOS serial number is the factory number of lenovo desktop, I think the automatic acquisition of the host number in the warranty page of lenovo should also call the "Win32_BIOS" of "SerialNumber"

Repair service page url: http: / / support1 lenovo. com. cn/lenovo/wsi/wsbx lenovo / # minarepairInfo


//BIOS information 
        private static string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")          //BIOS Manufacturer's name 
                    + identifier("Win32_BIOS", "SMBIOSBIOSVersion")  //
                    + identifier("Win32_BIOS", "IdentificationCode") //
                    + identifier("Win32_BIOS", "SerialNumber")       //BIOS The serial number 
                    + identifier("Win32_BIOS", "ReleaseDate")        // Manufacture date 
                    + identifier("Win32_BIOS", "Version");           // The version number 
        }

Get hard disk information:


private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")           // model 
                    + identifier("Win32_DiskDrive", "Manufacturer") // manufacturers 
                    + identifier("Win32_DiskDrive", "Signature")    // The signature 
                    + identifier("Win32_DiskDrive", "TotalHeads");  // Sector head 
        }

Get graphics card information:


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

Get network card MAC address information:


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


Related articles: