Search code examples
c#cmdwmic

How to retrieve hardware info in c#?


I want to export the result of command 'wmic bios get' to a xml file. How can I read these data and add each data as a xml node?


Solution

  • Something that actually had to do some time ago First you need class to handle requests by using wmi :

    public class GetHW
        {
            public 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")
                    {
                        //Only get the first one
                        if (result == "")
                        {
                            try
                            {
                                result = mo[wmiProperty].ToString();
                                break;
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                return result;
            }
    
           public 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)
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
                return result;
            }
        }
    

    Then in your actual application query class like this :

    GetHW HW = new GetHW();
    Dictionary <string, string> data = new Dictionary<string,string();
    data.Add("Manufacturer", HW.identifier("Win32_BIOS", "Manufacturer"));
    data.Add("SMBIOSBIOSVersion", HW.identifier("Win32_BIOS", "SMBIOSBIOSVersion"));
    data.Add("IdentificationCode", HW.identifier("Win32_BIOS", "IdentificationCode"));
    data.Add("SerialNumber", HW.identifier("Win32_BIOS", "SerialNumber"));
    data.Add("ReleaseDate", HW.identifier("Win32_BIOS", "ReleaseDate"));
    data.Add("Version", HW.identifier("Win32_BIOS", "Version"));
    

    Then do whatever you want with dictionary. It might be overkill for bios data, but it will allow you to pull other data if you ever need to - e.g. motherboard :

    data.Add("Model", HW.identifier("Win32_BaseBoard", "Model"));
    data.Add("Manufacturer", HW.identifier("Win32_BaseBoard", "Manufacturer"));
    data.Add("Name", HW.identifier("Win32_BaseBoard", "Name"));
    data.Add("SerialNumber", HW.identifier("Win32_BaseBoard", "SerialNumber"));
    

    or CPU

    data.Add("Unique ID", HW.identifier("Win32_Processor", "UniqueId")); 
    data.Add("ID", HW.identifier("Win32_Processor", "ProcessorId"));
    data.Add("Name", HW.identifier("Win32_Processor", "Name"));
    data.Add("Manufacturer", HW.identifier("Win32_Processor", "Manufacturer"));
    data.Add("MaxClockSpeed", HW.identifier("Win32_Processor", "MaxClockSpeed"));
    

    Edit : In case anyone ever need to - here is a a list of all win32 classes.