Reputation: 121
I'm in the process of creating a personal monitoring program for system performance, and I'm having issues figuring out how C# retrieves CPU and GPU Temperature information.
I already have the program retrieve the CPU Load and Frequency information(as well as various other things) through PerformanceCounter, but I haven't been able to find the Instance, Object,and Counter variables for CPU temp.
Also, I need to be able to get the temperature of more than one GPU, as I have two.
What do I do?
Upvotes: 9
Views: 18043
Reputation: 29
// 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);
}
Upvotes: 2
Reputation: 129
You can get the CPU temp in both WMI and Openhardwaremonitor way.
Open Hardwaremonitor:
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();
}
}
}
}
WMI:
using System;
using System.Diagnostics;
using System.Management;
class Program
{
static void Main(string[] args)
{
Double CPUtprt = 0;
System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(@"root\WMI", "Select * From MSAcpi_ThermalZoneTemperature");
foreach (System.Management.ManagementObject mo in mos.Get())
{
CPUtprt = Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10;
Console.WriteLine("CPU temp : " + CPUtprt.ToString() + " °C");
}
}
}
I found a nice tutorial here, I get the CPU temp successfully.
http://www.lattepanda.com/topic-f11t3004.html
Upvotes: 1
Reputation: 8940
You can use the WMI for that, there is a c# code generator for WMI that helps a lot when creating WMI quires as it is not documented that well.
The WMI code generator can be found here: http://www.microsoft.com/en-us/download/details.aspx?id=8572
a quick try generates something like this:
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);
}
}
This may not be exactly what you need just try around with the properties and classes available
Upvotes: 4