我有一个简单的问题。我无法读取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();
这里我得到一个例外,但上面的列表工作,并显示'CPU'。
Exeption指向';' Get()。详情:(使用德国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#
的新手答案 0 :(得分:2)
您似乎将错误的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();
}
}
}