-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenHardwareMonitorQuery.cs
59 lines (53 loc) · 2.13 KB
/
OpenHardwareMonitorQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
/* A simple class for making query of Open Hardware Monitor WMI Service */
namespace PluginOpenHardwareMonitor
{
class OpenHardwareMonitorQuery
{
public double query(string hwName, string sensorName, string sensorType)
{
//Need to first specify the Hardware name and the name of the sensor then sensor type
//Error checking SensorType or Name is not in specific HardwareName
string queryStr;
ManagementObject result = new ManagementObject();
//First get HardwareIdentifier by name
string strHwID = getHardwareIdentifierByName(hwName);
queryStr = "SELECT value FROM sensor WHERE " + "Parent=\"" + strHwID + "\" AND Name=\"" + sensorName + "\" AND SensorType=\"" + sensorType + "\"";
ManagementObjectCollection mCollection = makeQuery(queryStr);
if (mCollection.Count == 1) //Expecting only one return value
{
foreach (ManagementObject m in makeQuery(queryStr))
{
result = m;
break;
}
return Convert.ToDouble(result["Value"]);
}
else
{
return -1.0;
}
}
private ManagementObjectCollection makeQuery(string queryStr)
{
ManagementScope scope = new ManagementScope("root\\OpenHardwareMonitor");
ObjectQuery query = new ObjectQuery(queryStr);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
return searcher.Get();
}
private string getHardwareIdentifierByName(string hwName)
{
ManagementObject result = null;
var queryStr = "SELECT identifier FROM hardware WHERE " + "Name=\"" + hwName + "\"";
foreach (ManagementObject mgtObj in makeQuery(queryStr))
{
result = mgtObj;
break;
}
return result["Identifier"].ToString();
}
}
}