22

For a programming project I would like to access the temperature readings from my CPU and GPUs. I will be using C#. From various forums I get the impression that there is specific information and developer resources you need in order to access that information for various boards. I have a MSI NF750-G55 board. MSI's website does not have any of the information I am looking for. I tried their tech support and the rep I spoke with stated they do not have any such information. There must be a way to obtain that info.

Any thoughts?

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Paul
  • 347
  • 1
  • 2
  • 12
  • 1
    There is a decent sample project at http://geekswithblogs.net/cicorias/archive/2006/11/22/97855.aspx that might help you get started. Direct link to the zip file containing the solution and all sources: http://www.cicoria.com/downloads/TemperatureMonitor/TempMonitorSrc.zip – Chris Shouts May 27 '10 at 19:11
  • 2
    Using Justin Niessner's solution lot of people got System.InvalidOperationException. In order to avoid it, the program should be run in administrative mode. –  May 07 '13 at 18:21

3 Answers3

20

For at least the CPU side of things, you could use WMI.

The namespace\object is root\WMI, MSAcpi_ThermalZoneTemperature

Sample Code:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("root\\WMI",
                                 "SELECT * FROM MSAcpi_ThermalZoneTemperature");

ManagementObjectCollection collection = 
    searcher.Get();

foreach(ManagementBaseObject tempObject in collection)
{
    Console.WriteLine(tempObject["CurrentTemperature"].ToString());
}

That will give you the temperature in a raw format. You have to convert from there:

kelvin = raw / 10;

celsius = (raw / 10) - 273.15;

fahrenheit = ((raw / 10) - 273.15) * 9 / 5 + 32;
slawekwin
  • 6,270
  • 1
  • 44
  • 57
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I receive this error: 'enumerator.Current' threw an exception of type 'System.InvalidOperationException' – Aaron May 27 '10 at 19:30
  • @Aaron - Interesting. I wonder if your motherboard manufacturer supports this WMI monitoring (not all support it). – Justin Niessner May 27 '10 at 19:33
  • @Aaron - I'm a newb and am working on implementing the code. So will see =]. – Paul May 28 '10 at 00:51
  • I figured out how to implement the code however it I got an unhandled exception on while (enumerator.MoveNext()) – Paul May 28 '10 at 01:13
  • @Paul - For what it's worth, I did test the code before posting. It could be that your hardware doesn't support the monitoring. – Justin Niessner May 28 '10 at 01:49
  • 1
    I got a ManagementException not supported on the enumerator.MoveNext() anyone else get this? – ist_lion Nov 02 '10 at 19:58
  • 5
    Sadlyl, Most motherboards do not implement this via WMI. It throws an exception on my system as well. – Shachar Weis Jun 15 '12 at 14:58
  • This works sometimes and don't return neither CPU nor GPU temperature [read this](http://stackoverflow.com/questions/9101295/msacpi-thermalzonetemperature-class-not-showing-actual-temperature) I used word _sometimes_ because to get this value motherboard has to have at least one thermal sensor. – ST3 Jul 05 '13 at 14:17
  • 4
    I just want to point out that `MSAcpi_ThermalZoneTemperature` is NOT the CPU temp; but rather the temperature of an area of the motherboard... which is about as useful as measuring the temperature of a kitchen stove based on the temperature of the kitchen... – Electric Coffee Mar 07 '14 at 22:36
2

The best way to go for hardware related coding on windows is by using WMI which is a Code Creator tool from Microsoft, the tool will create the code for you based on what you are looking for in hardware related data and what .Net language you want to use.

The supported langauges currently are: C#, Visual Basic, VB Script.

Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
0

Note that MSAcpi_ThermalZoneTemperature does not give you the temperature of the CPU but rather the temperature of the motherboard. Also, note that most motherboards do not implement this via WMI.

You can give the Open Hardware Monitor a go, although it lacks support for the latest processors.

internal sealed class CpuTemperatureReader : IDisposable
{
    private readonly Computer _computer;

    public CpuTemperatureReader()
    {
        _computer = new Computer { CPUEnabled = true };
        _computer.Open();
    }

    public IReadOnlyDictionary<string, float> GetTemperaturesInCelsius()
    {
        var coreAndTemperature = new Dictionary<string, float>();

        foreach (var hardware in _computer.Hardware)
        {
            hardware.Update(); //use hardware.Name to get CPU model
            foreach (var sensor in hardware.Sensors)
            {
                if (sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue)
                    coreAndTemperature.Add(sensor.Name, sensor.Value.Value);
            }
        }

        return coreAndTemperature;
    }

    public void Dispose()
    {
        try
        {
            _computer.Close();
        }
        catch (Exception)
        {
            //ignore closing errors
        }
    }
}

Download the zip from the official source, extract and add a reference to OpenHardwareMonitorLib.dll in your project.

Ali Zahid
  • 1,329
  • 2
  • 17
  • 23