C# CPU and GPU Temp
Asked Answered
G

4

9

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?

Gaiety answered 13/4, 2015 at 14:16 Comment(3)
Have a look at OpenHardwareMonitor, written in C# and opensource.Metamerism
Have you tried any of the solutions suggested in this search, some of them have code snippets: google.co.uk/…Ines
possible duplicate of How to get CPU temperature?Nord
N
4

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

Nord answered 13/4, 2015 at 20:59 Comment(0)
T
2
// 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);
}
Tiltyard answered 1/4, 2018 at 11:19 Comment(5)
Add a bit more comment to itBud
PerformanceCounter is from System.Diagnostics namespace the rest is pretty much self explaining... what else to comment, really dont know... ASK and I will answer...Tiltyard
Instance '_TZ.THRM' does not exist in the specified CategoryBarny
@Giorgi: Neo is waiting for an answer Sep 14Haily
Source can be found here as on 28 May 2023. nuget.org/packages/System.Diagnostics.PerformanceCounterMaurist
S
1

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

Schiffman answered 28/1, 2018 at 16:33 Comment(3)
I would recommend using LibreHardwaremonitor (basically a fork of OpenHardwaremonitor) since it has less issues with the latest .net framework and also retrieves more sensors.Underlaid
Problem with LibreHardwaremonitor is that is doesn't seem to work with most CPUs I've found - no temps are ever reported. I couldn't get Haswell, nor Kaby Lake processors to work (everything except temp - go figure)Percaline
OpenHardwareManager.Core doesn't report CPU temperatures either. Anyone know of a library that supports the Haswell processors?Percaline
S
0

Use the MSAcpi_ThermalZoneTemperature class in WMI.

Suilmann answered 13/4, 2015 at 14:20 Comment(3)
This really requires a code sample and related instruction as WMI is not a C# construct.Gillum
@Gillum But there are plenty of C# WMI samples online if you search. This is definitely a step in the right direction.Intelligencer
I'll look around and see what I can get. I don't really need a complex block of code for this, I just need to retrieve the temps. After that I have everything handled. Hope WMI can give me that solution.Gaiety

© 2022 - 2024 — McMap. All rights reserved.