Get CPU Temperature using Open Hardware Monitor

安稳与你 提交于 2019-12-06 08:07:07

问题


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.


回答1:


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" />



回答2:


May be you have to Force your application to run as administrator, then your code may work.

Right click on Project > Add New Item, select "Application Manifest File".

Change the

<requestedExecutionLevel>

element to:

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

Here's the tutorial, you can have a look.

http://www.lattepanda.com/topic-f11t3004.html



来源:https://stackoverflow.com/questions/27296543/get-cpu-temperature-using-open-hardware-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!