1

我见过很多实例化 WMI 子类的例子Win32 Classes。例如,尝试查询 Win32_Battery 类:

using System.Management.Instrumentation;
...
ManagementClass mgmt = new ManagementClass("Win32_Battery");
ManagementObjectCollection o = mgmt.getInstances();

但是,此查询失败,它告诉我它是一个未知类,无法实例化。

我究竟做错了什么?

4

1 回答 1

1

Win32_PowerMeter 不在默认命名空间中。WMI Code Creator 实用程序生成的示例代码显示了正确的命名空间:

    ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher("root\\CIMV2\\power", 
        "SELECT * FROM Win32_PowerMeter"); 

    foreach (ManagementObject queryObj in searcher.Get())
    {
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("Win32_PowerMeter instance");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
    }
于 2012-06-12T09:25:51.177 回答