简体   繁体   中英

OpenhardwareMonitorLib System.IO.FileNotFoundException

I'm trying to read cpu temperature by using OpenhardwareMonitorLib.dll in c#. This is my code:

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;
        }
    }
}

I know that i need administartor privileges so this is my 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>

Now I'm sure that when i try to execute "computer.Open();"in TemperatureReader.cs i enter the catch block with this exception: "Eccezione generata: 'System.IO.FileNotFoundException' in OpenHardwareMonitorLib.dll". I can't figure it out how to resolve. If u need more info please tell me.

ps this is my.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>

and i can use OpenHardwareMonitor application an it works fine on my pc.

You can try this: Use Tools > NuGet Packed Menager > Manage Packet Solution and there browse for "LibreHardwareMonitorLibCore" ver 1.0.3 and install it. And maybe problem solved with you. That worked fine with me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM