【问题标题】:Having trouble getting CPU temps using Open Hardware Monitor C#使用 Open Hardware Monitor C# 获取 CPU 温度时遇到问题
【发布时间】:2021-02-07 12:16:42
【问题描述】:

正如标题所述,我无法使用 openhardwaremonitor.dll 参考获取 CPU 温度。我想我是以管理员身份运行程序,我在启动 Visual Studio 时选择以管理员身份运行,因为我没有添加 application.manifest 文件的选项。这是我正在使用的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;
namespace Get_CPU_Temp5
{
   class Program
   {
       public class UpdateVisitor : IVisitor
       {
           public void VisitComputer(IComputer computer)
           {
               computer.Traverse(this);
           }
           public void VisitHardware(IHardware hardware)
           {
               hardware.Update();
               foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
           }
           public void VisitSensor(ISensor sensor) { }
           public void VisitParameter(IParameter parameter) { }
       }
       static void GetSystemInfo()
       {
           UpdateVisitor updateVisitor = new UpdateVisitor();
           Computer computer = new Computer();
           computer.Open();
           computer.CPUEnabled = true;
           computer.Accept(updateVisitor);
           for (int i = 0; i < computer.Hardware.Length; i++)
           {
               if (computer.Hardware[i].HardwareType == HardwareType.CPU)
               {
                   for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                   {
                       if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                               Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                   }
               }
           }
           computer.Close();
       }
       static void Main(string[] args)
       {
           while (true)
           {
               GetSystemInfo();
           }
       }
   }
}

错误; “System.IO.FileNotFoundException: '无法加载文件或程序集'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'。系统找不到指定的文件。'”

这个错误在computer.Open();

我正在使用 Visual Studio 2019。 任何帮助将不胜感激!

【问题讨论】:

    标签: c# openhardwaremonitor


    【解决方案1】:

    我在尝试将 openhardwaremonitor.dll 与 .Net 核心一起使用时遇到了这个问题。我发现的解决方法是从 Nuget 加载 system.management。显然 system.management 程序集不像 .net Framework 那样包含在 .net 核心中。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多