问题
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