开发者

WMI GetPropertyQualifierValue "Not found"

开发者 https://www.devze.com 2023-01-05 05:19 出处:网络
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

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(); 开发者_C百科<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?


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

精彩评论

暂无评论...
验证码 换一张
取 消