我需要为我正在开发的应用程序收集一些系统信息.使用C#可以轻松获得可用内存和CPU负载.不幸的是,CPU温度并不那么容易.我尝试过使用WMI但是我无法使用任何东西
Win32_TemperatureProbe
Run Code Online (Sandbox Code Playgroud)
要么
MSAcpi_ThermalZoneTemperature
Run Code Online (Sandbox Code Playgroud)
有人已经处理过这个问题吗?我想知道像SiSoftware Sandra这样的监控程序如何获取这些信息......
如果有人感兴趣,这里是该类的代码:
public class SystemInformation
{
private System.Diagnostics.PerformanceCounter m_memoryCounter;
private System.Diagnostics.PerformanceCounter m_CPUCounter;
public SystemInformation()
{
m_memoryCounter = new System.Diagnostics.PerformanceCounter();
m_memoryCounter.CategoryName = "Memory";
m_memoryCounter.CounterName = "Available MBytes";
m_CPUCounter = new System.Diagnostics.PerformanceCounter();
m_CPUCounter.CategoryName = "Processor";
m_CPUCounter.CounterName = "% Processor Time";
m_CPUCounter.InstanceName = "_Total";
}
public float GetAvailableMemory()
{
return m_memoryCounter.NextValue();
}
public float GetCPULoad()
{
return m_CPUCounter.NextValue();
}
public float GetCPUTemperature()
{
//...
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我想实时获取 CPU 性能数据,包括温度。我使用以下代码来获取 CPU 温度:
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject queryObj in searcher.Get())
{
double temp = Convert.ToDouble(queryObj["CurrentTemperature"].ToString());
double temp_critical = Convert.ToDouble(queryObj["CriticalTripPoint"].ToString());
double temp_cel = (temp/10 - 273.15);
double temp_critical_cel = temp_critical / 10 - 273.15;
lblCurrentTemp.Text = temp_cel.ToString();
lblCriticalTemp.Text = temp_critical_cel.ToString();
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
Run Code Online (Sandbox Code Playgroud)
但此代码显示的温度不是正确的温度。它通常显示 49.5-50.5 摄氏度。但我使用了“OpenHardwareMonitor”,它报告 CPU 温度超过 71 摄氏度,并且随着时间分数的变化而变化。我在代码中遗漏了什么吗?
我在 timer_click 事件中每隔 500 毫秒使用上面的代码来刷新温度读数,但从执行开始它总是显示相同的温度。这意味着如果您运行此应用程序并且它显示 …