为了一个编程项目,我想访问我的CPU和GPU的温度读数。我将使用C#语言。从各种论坛中,我得到的印象是,你需要特定的信息和开发资源才能访问不同板块的这些信息。 我有一个MSI NF750-G55主板。MSI的网站上没有我要找的任何信息。我尝试了他们的技术支持,我跟那位代表谈过话后得知他们没有这样的信息。一定有办法获得这些信息。
有什么想法?
至少在处理 CPU 方面,您可以使用 WMI。
命名空间\对象是root\WMI, MSAcpi_ThermalZoneTemperature
示例代码:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature");
ManagementObjectCollection collection =
searcher.Get();
foreach(ManagementBaseObject tempObject in collection)
{
Console.WriteLine(tempObject["CurrentTemperature"].ToString());
}
这将给你以原始格式的温度。你需要从那里进行转换:
kelvin = raw / 10;
celsius = (raw / 10) - 273.15;
fahrenheit = ((raw / 10) - 273.15) * 9 / 5 + 32;
MSAcpi_ThermalZoneTemperature
不是CPU温度,而是主板某个区域的温度……这就像根据厨房的温度来测量炉子温度一样没有什么用处。 - Electric Coffee在Windows上进行与硬件相关的编码的最佳方法是使用微软的 WMI 工具,这是一个来自微软的代码生成器
工具,该工具将根据您所寻找的硬件相关数据和您想要使用的.Net语言为您创建代码。
目前支持的语言有:C#、Visual Basic和VB Script。
MSAcpi_ThermalZoneTemperature
并不会给出 CPU 的温度,而是主板的温度。此外,请注意大多数主板并未通过 WMI 实现此功能。internal sealed class CpuTemperatureReader : IDisposable
{
private readonly Computer _computer;
public CpuTemperatureReader()
{
_computer = new Computer { CPUEnabled = true };
_computer.Open();
}
public IReadOnlyDictionary<string, float> GetTemperaturesInCelsius()
{
var coreAndTemperature = new Dictionary<string, float>();
foreach (var hardware in _computer.Hardware)
{
hardware.Update(); //use hardware.Name to get CPU model
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue)
coreAndTemperature.Add(sensor.Name, sensor.Value.Value);
}
}
return coreAndTemperature;
}
public void Dispose()
{
try
{
_computer.Close();
}
catch (Exception)
{
//ignore closing errors
}
}
}