关于WMI的介绍可以参考https://www.cnblogs.com/zhaotianff/p/14764740.html
在C#中使用主要用到
ManagementClass和ManagementObjectSearcher两个类
示例代码如下:
1 internal class WMIHelper 2 { 3 internal static string GetWmiProperty(string wmiObject,string propertyName) 4 { 5 ManagementClass managementClass = new ManagementClass(wmiObject); 6 ManagementObjectCollection moc = managementClass.GetInstances(); 7 8 foreach (ManagementObject managementObject in moc) 9 { 10 return managementObject[propertyName].ToString(); 11 } 12 return ""; 13 } 14 15 internal static Dictionary<string,string> GetAllWmiProperty(string wmiObject) 16 { 17 Dictionary<string, string> dic = new Dictionary<string, string>(); 18 ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from " + wmiObject); 19 20 foreach (ManagementObject managementObject in managementObjectSearcher.Get()) 21 { 22 foreach (PropertyData property in managementObject.Properties) 23 { 24 dic.Add(property.Name, property.Value == null ? "" : property.Value.ToString()); 25 } 26 } 27 28 return dic; 29 } 30 }
例如获取CPU ID
1 internal static string GetCpuId() 2 { 3 return WMIHelper.GetWmiProperty("win32_processor", "processorid"); 4 }
获取CPU所有信息
1 WMIHelper.GetAllWmiProperty("win32_processor");
标签:wmiObject,return,string,C#,managementObject,WMI,internal,使用,property From: https://www.cnblogs.com/zhaotianff/p/17393907.html