1、数据结构
using System;namespace PcMonitor.Protocol
{[Serializable]public class MachineInfo{public CpuInfo Cpu;public MemoryInfo Memory;public GpuInfo Gpu;public MachineInfo(CpuInfo cpu, MemoryInfo memory, GpuInfo gpu){Cpu = cpu;Memory = memory;Gpu = gpu;}public override string ToString(){return $"{nameof(Cpu)}: {Cpu}, {nameof(Memory)}: {Memory}, {nameof(Gpu)}: {Gpu}";}}[Serializable]public class CpuInfo{public string Name;public Element Temperature;public Element Load;public Element Watt;public CpuInfo(string name, Element temperature, Element load, Element watt){Name = name;Temperature = temperature;Load = load;Watt = watt;}public override string ToString(){return $"{nameof(Name)}: {Name}, {nameof(Temperature)}: {Temperature}, {nameof(Load)}: {Load}, {nameof(Watt)}: {Watt}";}}[Serializable]public class MemoryInfo{public string Name;public Element Load;public MemoryInfo(string name, Element load){Name = name;Load = load;}public override string ToString(){return $"{nameof(Name)}: {Name}, {nameof(Load)}: {Load}";}}[Serializable]public class GpuInfo{public string Name;public Element Temperature;public Element Load;public Element Watt;public GpuInfo(string name, Element temperature, Element load, Element watt){Name = name;Temperature = temperature;Load = load;Watt = watt;}public override string ToString(){return $"{nameof(Name)}: {Name}, {nameof(Temperature)}: {Temperature}, {nameof(Load)}: {Load}, {nameof(Watt)}: {Watt}";}}[Serializable]public class Element{public string Type;public string Target;public float? Max;public float? Current;public Element(string type, string target, float? max, float? current){Type = type;Target = target;Max = max;Current = current;}public override string ToString(){return$"{nameof(Type)}: {Type}, {nameof(Target)}: {Target}, {nameof(Max)}: {Max}, {nameof(Current)}: {Current}";}}
}
2、帮助类
OpenHardwareMonitor允许您使用以下方式获取机器信息
基本用法如下。
OpenHardwareMonitor.Hardware.Computer创造
Computer.Open()称呼
IHardware得到你需要的东西
IHardware获取各种数值
using System;
using System.Linq;
using OpenHardwareMonitor.Hardware;
using PcMonitor.Protocol;namespace PcMonitor
{public sealed class HardwareObserver : IDisposable{private readonly Computer _computer = new();private readonly IHardware? _cpu;private readonly IHardware? _memory;private readonly IHardware? _gpu;public HardwareObserver(){_computer.Open();_computer.CPUEnabled = true;_computer.RAMEnabled = true;_computer.GPUEnabled = true;foreach (var hardware in _computer.Hardware){switch (hardware.HardwareType){case HardwareType.CPU:_cpu = hardware;break;case HardwareType.RAM:_memory = hardware;break;case HardwareType.GpuNvidia:case HardwareType.GpuAti:_gpu = hardware;break;}}}public MachineInfo GetMachineInfo(){_cpu?.Update();_memory?.Update();_gpu?.Update();var cpuInfo = GetCpuInfo();var memoryInfo = GetMemoryInfo();var gpuInfo = GetGpuInfo();return new MachineInfo(cpuInfo, memoryInfo, gpuInfo);}private GpuInfo? GetGpuInfo(){if (_gpu == null) return null;// GPU温度var gpuTemperature = _gpu.Sensors.FirstOrDefault(sensor =>sensor.SensorType == SensorType.Temperature && sensor.Name == "GPU Core");// 負荷var gpuLoad = _gpu.Sensors.FirstOrDefault(sensor => sensor.SensorType == SensorType.Load&& sensor.Name == "GPU Core");// 消費電力var gpuPower = _gpu.Sensors.FirstOrDefault(sensor => sensor.SensorType == SensorType.Power&& sensor.Name == "GPU Power");var gpuInfo = new GpuInfo(_gpu.Name,new Element("Temperature", gpuTemperature?.Name, gpuTemperature?.Max, gpuTemperature?.Value),new Element("Load", gpuLoad?.Name, gpuLoad?.Max, gpuLoad?.Value),new Element("Watt", gpuPower?.Name, gpuPower?.Max, gpuPower?.Value));return gpuInfo;}private MemoryInfo? GetMemoryInfo(){if (_memory == null) return null;var memoryLoad = _memory.Sensors.FirstOrDefault(sensor => sensor.SensorType == SensorType.Load);var memoryInfo = new MemoryInfo(_memory.Name,new Element("Memory Load", _memory.Name, memoryLoad?.Max, memoryLoad?.Value));return memoryInfo;}private CpuInfo? GetCpuInfo(){if (_cpu == null) return null;// CPU温度var cpuTemperature = _cpu.Sensors.FirstOrDefault(sensor =>sensor.SensorType == SensorType.Temperature && sensor.Name == "CPU Package");// CPU負荷var cpuLoad = _cpu.Sensors.FirstOrDefault(sensor => sensor.SensorType == SensorType.Load&& sensor.Name == "CPU Total");// CPU消費電力var cpuWatt = _cpu.Sensors.FirstOrDefault(sensor => sensor.SensorType == SensorType.Power&& sensor.Name == "CPU Package");var cpuInfo = new CpuInfo(_cpu.Name,new Element("Temperature", cpuTemperature?.Name, cpuTemperature?.Max, cpuTemperature?.Value),new Element("Load", cpuLoad?.Name, cpuLoad?.Max, cpuLoad?.Value),new Element("Watt", cpuWatt?.Name, cpuWatt?.Max, cpuWatt?.Value));return cpuInfo;}public void Dispose(){_computer.Close();}}
}
3、调用
using System;
using System.Threading;
using System.Threading.Tasks;
using PcMonitor;using var hardwareObserver = new HardwareObserver();while (true)
{var machineInfo = hardwareObserver.GetMachineInfo();Console.WriteLine(machineInfo);await Task.Delay(TimeSpan.FromSeconds(1));
}