6

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?

DanG
  • 91
  • 1
  • 1
  • 2
  • 6
    Have a look at OpenHardwareMonitor, written in C# and opensource. – leppie Apr 13 '15 at 14:18
  • Have you tried any of the solutions suggested in this search, some of them have code snippets: https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=get%20cpu%20temperature%20c%23 – t_plusplus Apr 13 '15 at 14:28
  • 1
    possible duplicate of [How to get CPU temperature?](http://stackoverflow.com/questions/1195112/how-to-get-cpu-temperature) – quadroid Apr 13 '15 at 21:03

4 Answers4

3

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

quadroid
  • 8,444
  • 6
  • 49
  • 82
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

haoming weng
  • 129
  • 1
  • 2
  • 1
    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. – Markus Michel Apr 14 '21 at 09:28
1
// 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);
}
Migol
  • 8,161
  • 8
  • 47
  • 69
0

Use the MSAcpi_ThermalZoneTemperature class in WMI.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24
  • 5
    This really requires a code sample and related instruction as WMI is not a C# construct. – Gusdor Apr 13 '15 at 14:22
  • @Gusdor But there are plenty of C# WMI samples online if you search. This is definitely a step in the right direction. – Rup Apr 13 '15 at 14:26
  • 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. – DanG Apr 13 '15 at 14:32