WMI查询的空引用处理

本文关键字:引用 处理 查询 WMI | 更新日期: 2023-09-27 17:50:11

我试图查询给定WMI类中的所有内容,但是每次我调整测试时,我都会得到一个空引用返回给我。理想情况下,我想使查询字符串是select * from Win32_BIOS,但我试图先把这个错误排序出来。当尝试Console.WriteLine(property)

时发生错误
public class PropertyValue
{
    public PropertyValue()
    {
    }
    public PropertyValue(string wmiClassName)
    {
        WmiClassName = wmiClassName;
    }
    public string WmiClassName { get; set; }

    public void TestString<T>(string propertyName)
    {
        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher
            (String.Format("SELECT {0} FROM {1}", propertyName, WmiClassName));
        var collection = moSearcher.Get();
        var enu = collection.GetEnumerator();
        {
            foreach (ManagementBaseObject prop in collection)
            {
                Type t = prop.Properties[propertyName].GetType();
                t.GetProperty(propertyName).GetValue(prop, null);
                Console.WriteLine(prop.Properties[propertyName].Value);
            }
        }
    }
    [TestClass]
    public class TestStrings
    {
        [TestMethod]
        public void TestThis()
        {
            PropertyValue propval = new PropertyValue("Win32_BIOS");
            propval.TestString<string>("Manufacturer");
        }
    }
}

WMI查询的空引用处理

问题是这台特定的机器在WMI中返回空值