Search code examples
wmiqualifiers

WMI GetPropertyQualifierValue "Not found"


My question is really simple, but apparently nobody's experienced a similar error. I'm writing a program to check if a WMI Class's property is writeable, that is, if the "Write" qualifier is true for that property. My code looks like this:

ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\\CIMV2", "SELECT * FROM " + "Win32_Processor"); <br />
ManagementObjectCollection moc= mos.Get(); <br />
ManagementClass manClass = new ManagementClass("Win32_Processor"); <br />

bool isWriteable = false;

isWriteable (bool)manClass.GetPropertyQualifierValue("Description", "Write"); <br />
// I've also tried to call it on a ManagementObject instance of ManagementObjectCollection, doesn't work either way

Every time it's called, however, it returns a "Not found" exception, regardless of which property or qualifier name I use (all of the one's I've tried I've pulled from MSDN — they should be valid).

Similarly, GetQualifierValue does not work, either, when trying to get the qualifiers of the class.

Anyone have any ideas?


Solution

  • The proper way to check if a Class’ property is writeable is to check for the existence of the “write” qualifier. The following is some sample code:

    ManagementClass processClass =
                    new ManagementClass("Win32_Process");
    
    bool isWriteable = false;
    foreach (PropertyData property in processClass.Properties)
    {
        if (property.Name.Equals("Description"))
        {
            foreach (QualifierData q in property.Qualifiers)
            {
                if (q.Name.Equals("write"))
                {
                    isWriteable = true;
                    break;
                }
            }
        }
    }
    

    Using the code below, you will see that the Description property only has the CIMTYPE, Description, and read qualifiers.

    ManagementClass processClass =
             new ManagementClass("Win32_Process");
    processClass.Options.UseAmendedQualifiers = true;
    
    foreach (PropertyData property in processClass.Properties)
    {
        if (property.Name.Equals("Description"))
        {
            foreach (QualifierData q in property.Qualifiers)
            {
                Console.WriteLine(q.Name);
            }
        }
    }