OpenhardwareMonitorLib System.IO.FileNotFoundException

问题描述

我正在尝试通过在 c# 中使用 OpenhardwareMonitorLib.dll 来读取 cpu 温度。这是我的代码

using System;
using System.Collections.Generic;
using System.Text;
using OpenHardwareMonitor.Hardware;

namespace FanController.Model {
    public class TemperatureReader {
        private Computer computer;

        public TemperatureReader() { }

        public void init() {
            this.computer = new Computer();
            this.computer.cpuEnabled = true;
            this.computer.GPUEnabled = true;
            try {
                this.computer.open();
            }
            catch {
                Console.WriteLine("### fail to open pc ###");
                return;
            }
            Console.Write(computer.GetReport());
        }

        public String getcpuTemperature() {
            String temperature = "prova";  //testing

            foreach (var hardware in this.computer.Hardware) {
                if (hardware.HardwareType == HardwareType.cpu) {
                    hardware.Update();
                    foreach (var sensor in hardware.Sensors) {
                        if (sensor.SensorType == SensorType.Temperature) {
                            temperature += String.Format("{0} Temperature = {1}\r\n",sensor.Name,sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
                        }
                    }
                }
            }
            Console.WriteLine(temperature);
            return temperature;
        }
    }
}

我知道我需要管理员权限,所以这是我的 app.manifest:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- Opzioni manifesto di Controllo dell'account utente
             Per modificare il livello di Controllo dell'account utente di Windows,sostituire il 
             nodo requestedExecutionLevel con uno dei seguenti.

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

            Se si specifica l'elemento requestedExecutionLevel,la funzionalità Virtualizzazione file system e registro di sistema verrà disabilitata. 
            Rimuovere questo elemento se l'applicazione richiede questa virtualizzazione per
            compatibilità con le versioni precedenti.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- Elenco delle versioni di Windows in cui è stata testata questa applicazione e
           per cui è stato previsto l'uso. Rimuovere il commento dagli elementi appropriati per
           fare in modo che Windows selezioni automaticamente l'ambiente più compatibile. -->

      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

      <!-- Windows 10 -->
      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

    </application>
  </compatibility>

  <!-- Indica che l'applicazione è sensibile ai valori DPI e non verrà scalata automaticamente da Windows in caso di
       valori DPI maggiori. Le applicazioni Windows Presentation Foundation (WPF) sono automaticamente sensibili ai valori DPI,pertanto non è necessario 
       acconsentire esplicitamente. Con le applicazioni Windows Form destinate a .NET Framework 4.6 per cui è stato acconsentito esplicitamente a questa impostazione,è anche necessario impostare 'EnableWindowsFormsHighDpiAutoResizing' su 'true' nel relativo file app.config. -->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
  -->

  <!-- Abilita i temi per finestre di dialogo e controlli comuni di Windows (Windows XP e versioni successive) -->
  <!--
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
  -->

</assembly>

现在我确定当我尝试执行“computer.open();”时在TemperatureReader.cs 中,我输入了带有此异常的catch 块:“Eccezione generata:'System.IO.FileNotFoundException' in OpenHardwareMonitorLib.dll”。我想不出如何解决。如果您需要更多信息,请告诉我。

附言这是我的 .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ApplicationManifest>app.manifest</ApplicationManifest>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="Lib\" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="OpenHardwareMonitorLib">
      <HintPath>Lib\OpenHardwareMonitorLib.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

而且我可以使用 OpenHardwareMonitor 应用程序,它在我的电脑上运行良好。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...