最近有軟件需求要獲取CPU 溫度等硬件參數,WMI 研究了幾天沒有找到方法,最後在網上找到openHardwareMonitor 可以實現。
我最初參考是以下網站:https://www.cnblogs.com/chen110xi/p/6189499.html
不過裏邊的代碼不能用:
1. 下載的OpenHardWareMonitor中包含了兩個工程,一個是Lib,一個是監測軟件。
2.按網站的說法,直接用Lib就可以了。而上邊的代碼中用到了app.config, 其他那個根本不用用在我們自己的工程中,那是給監測軟件 用。UI 的一些數。
3.在我的電腦上,能取到CPU 的核心數和name, 就是取不到溫度值。
4.後邊再研究了下,發現裏邊自帶的監測軟件是可以用, 可以正常顯示 溫度等值。我就奇怪了,這個軟件 能在我的電腦上正常工作,我的工程爲什麼不能呢。
5.進入到自帶的Monitor 軟件的mainForm中,發現有一個定時器,定時觸發刷新CPU 的溫度值。最後在源碼中發現有這麼一行(WmiProvider.cs中):
public void Update() {
foreach (IWmiObject instance in activeInstances)
instance.Update();
}
只有執行了這一行,溫度值纔會實現,不過for裏邊的update()函數是一個接口,看不到源碼,有高手看可找到源碼不,註釋中寫的:
namespace OpenHardwareMonitor.WMI {
interface IWmiObject {
// Both of these get exposed to WMI
string Name { get; }
string Identifier { get; }
// Not exposed.
void Update();
}
}
6.發現了這個函數後加到我的主函數裏,溫度就出來了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;
using OpenHardwareMonitor.WMI;
using System.Reflection;
using OpenHardwareMonitor;
using OpenHardwareMonitor.GUI;
// 注意,我因爲是用到了Monitor 原本的一些函數,所以在reference裏,不僅要引用 :OpenHardwareMonitorLib, 還要引用OpenHardwareMonitor, 可以引用dll, exe,也可以引用工程。
public static string GetTemperatureProbe()
{
StringBuilder str = new StringBuilder();
#if true
System.Version ver1 = Assembly.GetAssembly(typeof(Computer)).GetName().Version;
System.Version ver2 = Assembly.GetExecutingAssembly().GetName().Version;
//if (ver1 != ver2)
//{
// MessageBox.Show(
// "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
// "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Environment.Exit(0);
//}
PersistentSettings settings = new PersistentSettings();
UpdateVisitor updateVisitor = new UpdateVisitor();
//初始化config配置文件,詳情見Bin目錄Debug/Replase\OpenHardwareMonitor.config文件
string configPath = "D:\\3_git\\utility-tools\\WMIVerify\\WindowsFormsApplication1\\resource\\OpenHardware\\Bin\\Debug\\OpenHardwareMonitor.config";
settings.Load(configPath);
//初始化Computer對象
Computer computer= new Computer();
WmiProvider wmiProvider = new WmiProvider(computer);
//調用open方法
computer.Open();
//獲取cpu溫度:
computer.Accept(updateVisitor);
if (wmiProvider != null)
wmiProvider.Update();
for (int i = 0; i < computer.Hardware.Length; i++)
{
str.Append(string.Format("\nHardware type: {0}\n", computer.Hardware[i].HardwareType.ToString()));
string unitStr = "";
//循環找到HardwareType爲cpu
//if (computer.Hardware[i].HardwareType == HardwareType.CPU)
//{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
//找到溫度
str.Append(string.Format("Sensor type: {0} ", computer.Hardware[i].Sensors[j].SensorType.ToString()));
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
{
unitStr = " ℃";
}
else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Load)
{
unitStr = " %";
}
else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Clock)
{
unitStr = " MHz";
}
else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Voltage)
{
unitStr = " V";
}
else
{
unitStr = " ??";
}
//if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
//{
//獲取到cpu核心
str.Append(string.Format("name: {0} ", computer.Hardware[i].Sensors[j].Name));
str.Append("\t");
//這裏就是cpu溫度了
str.Append(string.Format("value: {0} {1}", computer.Hardware[i].Sensors[j].Value.ToString(), unitStr));
str.Append("\n");
//}
}
//}
}
#endif
return str + "";
}
7. 執行結果: