我有一个类,使用WMI从服务器收集信息。问题在于,某些版本的Windows似乎具有不同的可用/缺失属性,我无法找到一种方法来检查集合以查看值是否存在,然后再尝试访问它们的值。
明确一点,我已经发现可以通过使用wmiSingle.Properties.GetEnumerator()循环遍历整个集合并检查每个属性名称的值 - 但肯定有更好的方法。对吧?
明确一点,我已经发现可以通过使用wmiSingle.Properties.GetEnumerator()循环遍历整个集合并检查每个属性名称的值 - 但肯定有更好的方法。对吧?
ManagementScope wmiScope = new ManagementScope("\\\\MyLaptop\\root\\cimv2");
ObjectQuery wmiVolumeQuery = new System.Management.ObjectQuery("SELECT * FROM Win32_Processor");
using (ManagementObjectSearcher wmiObjectSearcher = new ManagementObjectSearcher(wmiScope, wmiVolumeQuery))
{
using (ManagementObjectCollection wmiMany = wmiObjectSearcher.Get())
{
foreach (ManagementObject wmiSingle in wmiMany)
{
Console.WriteLine(wmiSingle["Name"]);
//This line will throw an exception. How do I test to see if
// "SomeProperty" exists before attempting to access the value?
//Console.WriteLine(wmiSingle["SomeProperty"]);
object somePropertyValue = wmiSingle.GetPropertyValue("SomeProperty");
}
}
}