为什么我不能实例化Win32_PowerMeter WMI类?

时间:2012-06-12 08:15:49

标签: c# wmi power-management

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

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Win32_PowerMeter不在默认命名空间中。显示正确名称空间的WMI Code Creator utility生成的示例代码:

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