如何在C#中检索硬件信息?

时间:2019-07-17 15:18:16

标签: c# cmd wmic

我想将命令'wmic bios get'的结果导出到xml文件。如何读取这些数据并将每个数据添加为xml节点?

1 个答案:

答案 0 :(得分:0)

前一段时间实际上必须要做的事情 首先,您需要使用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;
        }
    }

然后在您的实际应用程序查询类中:

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"));

然后用字典做任何您想做的事情。 这可能对bios数据有些杀伤力,但是如果需要,它可以使您提取其他数据-例如主板:

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"));

或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"));

编辑:万一任何人需要-here是所有win32类的列表。