用C#获取CPU/GPU等系统信息遇异常,求解决方案
如何在C#中获取系统硬件信息并解决OpenHardwareMonitor异常问题
问题描述
- 如何在C#中获取CPU、GPU、RAM的使用率、温度、频率等系统信息?有没有简便方法?
- 如果没有简便方法,能否借助HWiNFO这类软件通过CMD提取这类信息(比如返回CPU温度)?
更新:使用OpenHardwareMonitor时遇到异常
找到一段代码,但运行时抛出未知异常,代码如下:
using OpenHardwareMonitor.Hardware; namespace Monitor { class Program { // 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(); } } } }
错误信息:
未处理的异常: System.MissingMethodException: 找不到方法:“System.Threading.Mutex System.Threading.Mutex.OpenExisting(System.String, System.Security.AccessControl.MutexRights)”。
在 OpenHardwareMonitor.Hardware.Ring0.Open()
在 OpenHardwareMonitor.Hardware.Computer.Open()
在 Monitor.Program.Main(String[] args) 位置 D:\VisualStudio\Monitor_Test\Monitor_Test\Program.cs:行号 136
解决方法
1. 匹配.NET框架版本
这个异常的核心原因是你使用的.NET框架版本不支持Mutex.OpenExisting(string, MutexRights)重载——该方法是**.NET Framework 4.5及以上**才新增的。
- 右键项目 → 属性 → 应用程序 → 目标框架,切换到.NET Framework 4.5或更高版本(推荐4.8)。
- 如果必须使用旧版本框架,需要找针对该版本编译的OpenHardwareMonitor分支或替代库。
2. 正确安装OpenHardwareMonitor库
- 避免手动复制旧版DLL,直接通过NuGet安装
OpenHardwareMonitorLib包,NuGet会自动匹配你的.NET框架版本,减少兼容性问题。 - 若手动引用DLL,确认DLL是对应当前框架版本编译的。
3. 确保程序拥有管理员权限
OpenHardwareMonitor读取硬件传感器需要管理员权限,即使解决了版本问题,权限不足也会导致数据读取失败:
- 以管理员身份运行Visual Studio,再编译运行程序。
- 或者给程序添加管理员权限清单:右键项目 → 属性 → 安全性 → 勾选“启用ClickOnce安全设置”,切换到安全选项卡选择“完全信任的应用程序”,保存后取消勾选“启用ClickOnce安全设置”,程序会自动生成权限清单。
替代方案:用HWiNFO通过CMD提取数据
如果不想折腾OpenHardwareMonitor,HWiNFO支持命令行导出硬件数据:
- 安装HWiNFO64,在设置→传感器中开启“远程传感器支持”。
- 使用CMD命令导出数据:
参数说明:hwinfo64.exe -sensor -log "C:\sensor_log.txt" -interval 1000-sensor:仅输出传感器数据-log:指定输出文件路径-interval:数据刷新间隔(单位:毫秒)
- 在C#中通过
Process类执行该命令,读取日志文件内容后,解析出需要的CPU/GPU温度、使用率等数据。
内容的提问来源于stack exchange,提问作者user20649315
相关产品推荐
相关产品推荐

