这是采集出来的Json,部分电脑(特别是笔记本)无法获取到:
{"HardwareList": [{"Name": "MITX-6999","Type": "主板","Sensors": [],"WmiReport": null}, {"Name": "Intel Core i5-8500","Type": "CPU","Sensors": [{"Name": "CPU Core #1","Type": "Load","Value": 7.142857,"Unit": "%"}, {"Name": "CPU Core #2","Type": "Load","Value": 7.142857,"Unit": "%"}, {"Name": "CPU Core #3","Type": "Load","Value": 7.142857,"Unit": "%"}, {"Name": "CPU Core #4","Type": "Load","Value": 7.142857,"Unit": "%"}, {"Name": "CPU Core #5","Type": "Load","Value": 14.2857141,"Unit": "%"}, {"Name": "CPU Core #6","Type": "Load","Value": 14.2857141,"Unit": "%"}, {"Name": "CPU Total","Type": "Load","Value": 9.523809,"Unit": "%"}, {"Name": "CPU Core #1","Type": "Temperature","Value": 45.0,"Unit": "°C"}, {"Name": "CPU Core #2","Type": "Temperature","Value": 44.0,"Unit": "°C"}, {"Name": "CPU Core #3","Type": "Temperature","Value": 43.0,"Unit": "°C"}, {"Name": "CPU Core #4","Type": "Temperature","Value": 44.0,"Unit": "°C"}, {"Name": "CPU Core #5","Type": "Temperature","Value": 44.0,"Unit": "°C"}, {"Name": "CPU Core #6","Type": "Temperature","Value": 43.0,"Unit": "°C"}, {"Name": "CPU Package","Type": "Temperature","Value": 45.0,"Unit": "°C"}, {"Name": "CPU Core #1","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Core #2","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Core #3","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Core #4","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Core #5","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Core #6","Type": "Clock","Value": 3900.00073,"Unit": "MHz"}, {"Name": "CPU Package","Type": "Power","Value": 15.735323,"Unit": "W"}, {"Name": "CPU Cores","Type": "Power","Value": 14.8387508,"Unit": "W"}, {"Name": "CPU Graphics","Type": "Power","Value": 0.04656077,"Unit": "W"}, {"Name": "CPU DRAM","Type": "Power","Value": 0.446761668,"Unit": "W"}, {"Name": "Bus Speed","Type": "Clock","Value": 100.000015,"Unit": "MHz"}],"WmiReport": null}, {"Name": "Generic Memory","Type": "内存","Sensors": [{"Name": "Memory","Type": "Load","Value": 28.5646687,"Unit": "%"}, {"Name": "Used Memory","Type": "Data","Value": 2.25409317,"Unit": "GB"}, {"Name": "Available Memory","Type": "Data","Value": 5.6371,"Unit": "GB"}],"WmiReport": null}, {"Name": "128GB SATA SSD","Type": "硬盘","Sensors": [{"Name": "Temperature","Type": "Temperature","Value": 31.0,"Unit": "°C"}, {"Name": "Used Space","Type": "Load","Value": 14.6201363,"Unit": "%"}],"WmiReport": "硬盘温度: 0°C\r\n硬盘温度: 1°C\r\n"}, {"Name": "ST1000VX012-3CU10C","Type": "硬盘","Sensors": [{"Name": "Temperature","Type": "Temperature","Value": 38.0,"Unit": "°C"}, {"Name": "Used Space","Type": "Load","Value": 0.06617792,"Unit": "%"}],"WmiReport": "硬盘温度: 0°C\r\n硬盘温度: 1°C\r\n"}],"ErrorMessage": null,"Success": true
}
下面是代码:
- 引用OpenHardwareMonitorLib.dll
- 调用
HardwareMonitor hardwareMonitor = new HardwareMonitor();HardwareMonitorModel hardwareMonitorModel = hardwareMonitor.GetHardwareInfo();
- 实现
public class HardwareMonitor
{private Computer computer;public HardwareMonitor(){computer = new Computer{CPUEnabled = true,GPUEnabled = true,HDDEnabled = true,MainboardEnabled = true,RAMEnabled = true // 新增内存监控};}public HardwareMonitorModel GetHardwareInfo(){var model = new HardwareMonitorModel();try{computer.Open();// 多次更新以确保数据稳定for (int i = 0; i < 3; i++){UpdateAllHardware();System.Threading.Thread.Sleep(100);}// 收集硬件信息foreach (var hardware in computer.Hardware){var hardwareInfo = new HardwareInfo{Name = hardware.Name,Type = GetHardwareTypeString(hardware.HardwareType),WmiReport = hardware.HardwareType == HardwareType.HDD ? GetHddWmiReport(hardware.Name) : null};// 处理主硬件传感器CollectSensors(hardware, hardwareInfo);// 处理子硬件传感器foreach (var subHardware in hardware.SubHardware){CollectSensors(subHardware, hardwareInfo);}// 特殊处理AMD GPU温度if (hardware.HardwareType == HardwareType.GpuAti){var amdGpuTemp = GetAmdGpuTemperature(hardware.Name);if (amdGpuTemp.HasValue){hardwareInfo.Sensors.Add(new SensorInfo{Name = "Temperature",Type = "Temperature",Value = amdGpuTemp.Value,Unit = "°C"});}}model.HardwareList.Add(hardwareInfo);}model.Success = true;}catch (Exception ex){model.ErrorMessage = $"获取硬件信息失败: {ex.Message}\n提示: 请确保以管理员权限运行程序";model.Success = false;}finally{computer.Close();}return model;}private void UpdateAllHardware(){foreach (var hardware in computer.Hardware){hardware.Update();// 特殊处理硬盘if (hardware.HardwareType == HardwareType.HDD){hardware.GetReport();}foreach (var subHardware in hardware.SubHardware){subHardware.Update();}}}private void CollectSensors(IHardware hardware, HardwareInfo hardwareInfo){foreach (var sensor in hardware.Sensors){if (sensor.Value.HasValue){hardwareInfo.Sensors.Add(new SensorInfo{Name = sensor.Name,Type = sensor.SensorType.ToString(),Value = sensor.Value,Unit = GetSensorUnit(sensor.SensorType)});}}}private float? GetAmdGpuTemperature(string gpuName){try{using (var searcher = new ManagementObjectSearcher(@"root\AMD\GPU", "SELECT * FROM GPU_Thermal")){foreach (var obj in searcher.Get()){if (obj["DeviceName"].ToString().Contains(gpuName)){return Convert.ToSingle(obj["Temperature"]);}}}}catch { }return null;}private string GetHddWmiReport(string hddName){var report = new StringBuilder();try{using (var searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSStorageDriver_FailurePredictData")){foreach (var obj in searcher.Get()){byte[] data = (byte[])obj["VendorSpecific"];if (data != null && data.Length >= 10){// SMART属性194通常是温度byte temperature = data[10];report.AppendLine($"硬盘温度: {temperature}°C");}}}}catch { }return report.Length > 0 ? report.ToString() : null;}private string GetHardwareTypeString(HardwareType type){switch (type){case HardwareType.CPU: return "CPU";case HardwareType.GpuNvidia: return "NVIDIA GPU";case HardwareType.GpuAti: return "AMD GPU";case HardwareType.HDD: return "硬盘";case HardwareType.Mainboard: return "主板";case HardwareType.RAM: return "内存";default: return type.ToString();}}private string GetSensorUnit(SensorType type){switch (type){case SensorType.Temperature: return "°C";case SensorType.Fan: return "RPM";case SensorType.Voltage: return "V";case SensorType.Clock: return "MHz";case SensorType.Load:case SensorType.Control: return "%";case SensorType.Power: return "W";case SensorType.Data: return "GB";case SensorType.Factor: return "";default: return "";}}
}
- 使用到的Model对象
public class HardwareInfo{public string Name { get; set; }public string Type { get; set; }public List<SensorInfo> Sensors { get; set; }public string WmiReport { get; set; }public HardwareInfo(){Sensors = new List<SensorInfo>();}}public class HardwareMonitorModel{public List<HardwareInfo> HardwareList { get; set; }public string ErrorMessage { get; set; }public bool Success { get; set; }public HardwareMonitorModel(){HardwareList = new List<HardwareInfo>();}}public class SensorInfo{public string Name { get; set; }public string Type { get; set; }public float? Value { get; set; }public string Unit { get; set; }}