使用Open Hardware Monitor获取CPU温度

3
我正在尝试使用OpenHardwareMonitorLib DLL来获取我的CPU/核心温度,但是这个方法对我不起作用。我已经查看了很多信息并发现这几乎是一个普遍的问题,但是我仍然无法使其正常工作。如果有人可以告诉我出错的地方,我将非常感激。这是我的代码:
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);

                    }

                }
            }
        }
    }
}

我的终极目标是将其与一个更大的应用程序结合起来。


1
OHM 应用程序是否正常工作? - leppie
浏览了一些其他OpenHardwareMonitor标记的问题后,我发现最近的OpenHardwareMonitor示例代码C#其中一个答案建议由于API调用的性质,您需要以管理员权限运行VS。 - AWinkle
@Simon:编辑正确地从问题中删除了你的签名,因为你的用户名已经被SO自动显示在问题的右下角。 - Ben Voigt
如果昨天我做了那个的话,我本来可以完成这个任务的……我猜这个程序也需要提升权限。 - Simon Price
我只是因为这个努力的新奇而点赞。听起来很酷! - code4life
1个回答

3

您需要在应用程序中请求更高的执行级别,以便此代码可以正常运行。

要做到这一点,您需要:

  • 右键单击项目;
  • 点击添加
  • 点击新建项...
  • 在搜索栏上键入清单
  • 点击确定

之后,您需要在清单中更改这一行:

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

转化为:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接