[英]C# - How to get a CPU Temp / CPU Load / Current Voltage / VID Voltage / Current Frequency with LibreHardwareMonitor?
對於我的 C#.Net Framework 項目,我需要獲得 5 個值:
我試過 WMI,但它不適用於現代 CPU,並且無法正確獲取這些值。 在做了一些研究之后,我認為獲取這些值的最佳方法是使用 OpenHardwareMonitor 或 LibreHardwareMonitor ( Git )。
因此,我安裝了 LibreHardwareMonitor NuGet,它添加了所有引用和 GitHub 編譯的示例代碼,但控制台立即關閉。
從下面的示例代碼中,您可以看出它輸出了它可以獲得的每個指標,而我只需要上面提到的那 5 個值。
如果有人有使用 LibreHardwareMonitor 或 OpenHardwareMonitor 的經驗,如果你能告訴我如何獲得我需要的那些值,我將不勝感激。
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) { }
}
public void Monitor()
{
Computer computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsMemoryEnabled = true,
IsMotherboardEnabled = true,
IsControllerEnabled = true,
IsNetworkEnabled = true,
IsStorageEnabled = true
};
computer.Open();
computer.Accept(new UpdateVisitor());
foreach (IHardware hardware in computer.Hardware)
{
Console.WriteLine("Hardware: {0}", hardware.Name);
foreach (IHardware subhardware in hardware.SubHardware)
{
Console.WriteLine("\tSubhardware: {0}", subhardware.Name);
foreach (ISensor sensor in subhardware.Sensors)
{
Console.WriteLine("\t\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
}
}
foreach (ISensor sensor in hardware.Sensors)
{
Console.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
}
}
computer.Close();
}
只需將此代碼與 OpenHardwareMonitor 一起使用。 然后從ReportSystemInfo()中提取cpuUsage、cpuTemp等;
using System;
using OpenHardwareMonitor.Hardware;
namespace PPSU_hwmonitor_c
{
class Program
{
/**
* Define vars to hold stats
**/
// CPU Temp
static float cpuTemp;
// CPU Usage
static float cpuUsage;
// CPU Power Draw (Package)
static float cpuPowerDrawPackage;
// CPU Frequency
static float cpuFrequency;
// GPU Temperature
static float gpuTemp;
// GPU Usage
static float gpuUsage;
// GPU Core Frequency
static float gpuCoreFrequency;
// GPU Memory Frequency
static float gpuMemoryFrequency;
/**
* Init OpenHardwareMonitor.dll Computer Object
**/
static Computer c = new Computer()
{
GPUEnabled = true,
CPUEnabled = true,
//RAMEnabled = true, // uncomment for RAM reports
//MainboardEnabled = true, // uncomment for Motherboard reports
//FanControllerEnabled = true, // uncomment for FAN Reports
//HDDEnabled = true, // uncomment for HDD Report
};
/**
* Pulls data from OHM
**/
static void ReportSystemInfo()
{
foreach (var hardware in c.Hardware)
{
if (hardware.HardwareType == HardwareType.CPU)
{
// only fire the update when found
hardware.Update();
// loop through the data
foreach (var sensor in hardware.Sensors)
if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("CPU Package"))
{
// store
cpuTemp = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("cpuTemp: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("CPU Total"))
{
// store
cpuUsage = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("cpuUsage: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Power && sensor.Name.Contains("CPU Package"))
{
// store
cpuPowerDrawPackage = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("CPU Power Draw - Package: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("CPU Core #1"))
{
// store
cpuFrequency = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("cpuFrequency: " + sensor.Value.GetValueOrDefault());
}
}
// Targets AMD & Nvidia GPUS
if (hardware.HardwareType == HardwareType.GpuAti || hardware.HardwareType == HardwareType.GpuNvidia)
{
// only fire the update when found
hardware.Update();
// loop through the data
foreach (var sensor in hardware.Sensors)
if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("GPU Core"))
{
// store
gpuTemp = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("gpuTemp: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("GPU Core"))
{
// store
gpuUsage = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("gpuUsage: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Core"))
{
// store
gpuCoreFrequency = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("gpuCoreFrequency: " + sensor.Value.GetValueOrDefault());
}
else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Memory"))
{
// store
gpuMemoryFrequency = sensor.Value.GetValueOrDefault();
// print to console
System.Diagnostics.Debug.WriteLine("gpuMemoryFrequency: " + sensor.Value.GetValueOrDefault());
}
}
// ... you can access any other system information you want here
}
}
static void Main(string[] args)
{
c.Open();
// loop
while (true)
{
ReportSystemInfo();
}
}
}
}
我試過你的答案,但對於溫度和 cpu 時鍾(可能還有其他),結果是 0。這是我的輸出:
CPU使用率:5,867474
CPU溫度:0
CPU頻率:0
顯卡溫度:28
GPU核心頻率:1139
gpu內存頻率:4303,992
GPU使用率:1
結束報告
我錯過了什么?
供參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.