c# - 从非默认命名空间读取 WMI 类

标签 c# namespaces scope wmi

我有一个简单的问题。我无法读取 WMI 类的特定实例。

ManagementScope scope = new ManagementScope(@"\\.\root\OpenHardwareMonitor", null);
scope.Connect();
ManagementPath printersPath = new ManagementPath("Sensor");            
ObjectGetOptions options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);

ManagementClass printersManager = new ManagementClass(scope, printersPath, options);
foreach (ManagementObject printer in printersManager.GetInstances())
{
    Console.WriteLine(printer["Name"]);
}

ManagementPath path = new ManagementPath(scope.Path + ":Sensor.Name='CPU'");

ManagementObject disk = new ManagementObject();
disk.Path = path;
disk.Get();

在这里我得到一个异常(exception),但上面的列表有效并且显示了“CPU”。

Exeption 指向“;”的获取()。详细信息:(使用德国VS12)
System.Management.ManagementException wurde nicht behandelt.
  HResult=-2146233087
  Message=Ungültiger Objektpfad <--- invalid objektpath
  Source=System.Management
  StackTrace:
       bei System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
       bei System.Management.ManagementObject.Get()
       bei Hello_openHWM.Program.getCPUTemp() in c:\Users\XXX\Documents\Visual Studio 2012\Projects\Hello openHWM\Hello openHWM\Program.cs:Zeile 46.
       bei Hello_openHWM.Program.Main(String[] args) in c:\Users\XXX\Documents\Visual Studio 2012\Projects\Hello openHWM\Hello openHWM\Program.cs:Zeile 18.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我究竟做错了什么?我是 C# 的新手

最佳答案

看来你传错了WMI object path到 WMI,作为解决方法,您可以使用 WQL语句来检索正确的数据。

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\OpenHardwareMonitor", "."), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Sensor Where Name LIKE 'CPU%'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","Identifier",WmiObject["Identifier"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Index",WmiObject["Index"]);// Sint32
                    Console.WriteLine("{0,-35} {1,-40}","InstanceId",WmiObject["InstanceId"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Max",WmiObject["Max"]);// Real32
                    Console.WriteLine("{0,-35} {1,-40}","Min",WmiObject["Min"]);// Real32
                    Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Parent",WmiObject["Parent"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","ProcessId",WmiObject["ProcessId"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","SensorType",WmiObject["SensorType"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Value",WmiObject["Value"]);// Real32

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

关于c# - 从非默认命名空间读取 WMI 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848323/

相关文章:

javascript - JS中变量的作用域

javascript - 从页面 HTML 获取动态元素?

c# - 如何为字符串生成 GUID?

c# - Windows 应用商店中的 GetInterfaces

c# - 在 ASP.NET MVC 中路由到 Root View

r - 在不附加包的情况下评估包环境中的功能

C# 命名空间别名限定符 (::) 与取消引用运算符 (.)

用于扩展另一个产品的 C# 命名空间

python - 如何正确命名变量以避免在 Python 中出现类似 "Shadows name from outer scope"的警告

javascript - 从内部函数中的父作用域访问数组元素