Get CPU Temperature using Open Hardware Monitor

Simon Price picture Simon Price · Dec 4, 2014 · Viewed 9.7k times · Source

I am trying to use the OpenHardwareMonitorLib DLL to get the temperature of my CPU \ cores, however this doesn't return the temperature for me.

I have looked around and seen that this is a problem almost everywhere but I cannot get this to work.

I would be very appreciative if someone can tell me where I am going wrong with this.

This is my code:

using System;
using System.Linq;
using System.Management;
using OpenHardwareMonitor.Collections;
using OpenHardwareMonitor.Hardware;
using OxyPlot;
using OxyPlot.Series;


namespace cs_TempReader
{
    class Program
    {
        private DateTime now;
        protected readonly ListSet<ISensor> active = new ListSet<ISensor>();
        public event SensorEventHandler SensorAdded;
        public event SensorEventHandler SensorRemoved;

        protected virtual void ActivateSensor(ISensor sensor)
        {
            if (active.Add(sensor))
                if (SensorAdded != null)
                    SensorAdded(sensor);
        }

        private static void Main(string[] args)
        {
            var myComputer = new Computer();

            myComputer.CPUEnabled = true;
            myComputer.ToCode();
            myComputer.Open();

            foreach (var hardwareItem in myComputer.Hardware)
            {
                hardwareItem.Update();
                hardwareItem.GetReport();

                Console.WriteLine(hardwareItem.GetReport());

                var series = new LineSeries();

                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Temperature)
                    {
                        Console.WriteLine("{0} {1} {2} = {3}", sensor.Name, sensor.Hardware, sensor.SensorType, sensor.Value);

                    }

                }
            }
        }
    }
}

My ultimate goal is to be able to tie this into a bigger application.

Answer

Gabriel Brito picture Gabriel Brito · Jun 18, 2016

You need to request a higher execution level in the application so this code can work properly.

To do this you have to:

  • Right click on the project;
  • Click on Add
  • Click on New Item...
  • Type manifest on the search bar
  • Click on Ok

After that you have to change this line on the manifest:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To this:

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />