我正在为系统性能创建个人监控程序,而且我在解决C#如何检索CPU和GPU温度信息时遇到了问题。
我已经让程序通过PerformanceCounter检索CPU负载和频率信息(以及各种其他内容),但是我无法找到CPU temp的Instance,Object和Counter变量。
另外,我需要能够获得多个GPU的温度,因为我有两个。
我该怎么办?
答案 0 :(得分:2)
你可以使用WMI,有一个用于WMI的c#代码生成器,在创建WMI quires时有很多帮助,因为它没有很好地记录。
可在此处找到WMI代码生成器:http://www.microsoft.com/en-us/download/details.aspx?id=8572
快速尝试生成如下内容:
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSAcpi_ThermalZoneTemperature instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("CurrentTemperature: {0}", queryObj["CurrentTemperature"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
这可能不是您需要的,只需尝试使用可用的属性和类
答案 1 :(得分:0)
在WMI中使用MSAcpi_ThermalZoneTemperature
类。
答案 2 :(得分:0)
您可以使用WMI和Openhardwaremonitor方式获取CPU temp。
打开Hardwaremonitor:
$.getJSON("rest/platforms", function( platforms ) {
platforms.forEach(function( platform ){
platform.name; // sitecore
platform.tasks.forEach(function( task ){
task.name; // promobox
task.time; // 10
});
});
});
WMI:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;
namespace Get_CPU_Temp5
{
class Program
{
public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
}
static void GetSystemInfo()
{
UpdateVisitor updateVisitor = new UpdateVisitor();
Computer computer = new Computer();
computer.Open();
computer.CPUEnabled = true;
computer.Accept(updateVisitor);
for (int i = 0; i < computer.Hardware.Length; i++)
{
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
}
}
}
computer.Close();
}
static void Main(string[] args)
{
while (true)
{
GetSystemInfo();
}
}
}
}
我在这里找到了一个很好的教程,我成功获得了CPU temp。
答案 3 :(得分:0)
// Gets temperature info from OS and prints it to the console
PerformanceCounter perfCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
PerformanceCounter tempCount = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM");
while (true)
{
Console.WriteLine("Processor time: " + perfCount.NextValue() + "%");
// -273.15 is the conversion from degrees Kelvin to degrees Celsius
Console.WriteLine("Temperature: {0} \u00B0C", (tempCount.NextValue() - 273.15f));
Thread.Sleep(1000);
}