3

I have a class which collects information from servers using WMI. The problem is that some versions of windows seem to have different properties available/missing and I cannot seem to find a way to check the collection to see if a value exists before I attempt to access their value.

To be clear, I have found it possible to loop through the entire collection and check each property name value by using wmiSingle.Properties.GetEnumerator() - but there must be a better way. Right?

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

2 Answers 2

8

I believe the only way to check this is to iterate through the Properties

foreach (var prop in wmiSingle.Properties)
{
    if(prop.Name == "SomeProperty")
    { /* do something */ }        
}

You could also just catch the exception - like this

public static class Extensions
{
    public static object TryGetProperty(this System.Management.ManagementObject wmiObj, string propertyName)
    {
        object retval;
        try
        {
            retval = wmiObj.GetPropertyValue(propertyName);
        }
        catch (System.Management.ManagementException ex)
        {
            retval = null;
        }
        return retval;
    }
}

Intentionally / knowingly causing exceptions to be thrown is typically not efficient; however, neither is iterating through an entire collection looking for a single property.

1
  • 1
    I believe you are right, but I hope you are wrong. I'll give it another day then accept.
    – NTDLS
    Aug 4, 2014 at 22:12
4

In your specific situation I would store the names of all the properties in a HashSet and check against that. That is incredibly fast. Especially if you are going to check more than once during the lifetime of your program.

HashSet<string> KnownProperties = new HashSet<string>() { };

Then once populate it in an iteration through the properties

foreach (PropertyData prop in mo.Properties)
{
     KnownProperties.Add(prop.Name);
}

Now you can get the value incredibly fast (I tested it and got 60 ticks for 100 iterations = 60ns!)

string Value = KnownProperties.Contains(PropertyName) ? mo[PropertyName].ToString() : "";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.