WMI GetPropertyQualifierValue“未找到”

发布于 2024-09-06 21:11:10 字数 783 浏览 5 评论 0原文

我的问题非常简单,但显然没有人遇到过类似的错误。我正在编写一个程序来检查 WMI 类的属性是否可写,即该属性的“Write”限定符是否为 true。我的代码如下所示:

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

但是,每次调用它时,它都会返回“未找到”异常,无论我使用哪个属性或限定符名称(我尝试过的所有属性或限定符名称都是从 MSDN 中提取的 - 它们应该是有效的)。

同样,在尝试获取类的限定符时,GetQualifierValue 也不起作用。

有人有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2024-09-13 21:11:10

检查类属性是否可写的正确方法是检查“write”限定符是否存在。以下是一些示例代码:

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

使用下面的代码,您将看到 Description 属性仅具有 CIMTYPEDescriptionread 限定符。

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

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文