using System;
using System.Linq;
using LibreHardwareMonitor.Hardware;
namespace VdsHealthDegree
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Computer computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsStorageEnabled = true,
IsMotherboardEnabled = true,
IsControllerEnabled = true
};
computer.Open();
foreach (var hardware in computer.Hardware)
{
Console.WriteLine("Hardware: {0}", hardware.Name);
UpdateHardware(hardware);
}
computer.Close();
}
public static void UpdateHardware(IHardware hardware)
{
hardware.Update();
foreach (var subHardware in hardware.SubHardware)
{
UpdateHardware(subHardware);
}
foreach (var sensor in hardware.Sensors)
{
switch (sensor.SensorType)
{
case SensorType.Temperature:
Console.WriteLine("Temperature Sensor: {0} - Value: {1} °C", sensor.Name, sensor.Value);
break;
case SensorType.Fan:
Console.WriteLine("Fan Sensor: {0} - Value: {1} RPM", sensor.Name, sensor.Value);
break;
}
}
}
// public void GetHardwareStatus()
// {
// Computer myComputer = new Computer();
// // 添加要监控的硬件,若不添加,默认监控所有
// // myComputer.OpenHardwareMonitor.Hardware.ComputerBase.HardwareAdded += Hardware_Added;
// myComputer.CPUEnabled = true;
// myComputer.GPUEnabled = true;
// myComputer.HDDEnabled = true;
// myComputer.MainboardEnabled = true;
//
// myComputer.Open();
//
// // 等待数据更新
// while (true)
// {
// foreach (var hardwareItem in myComputer.Hardware)
// {
// if (hardwareItem.HardwareType == HardwareType.CPU)
// {
// Console.WriteLine("CPU Temperature: " + hardwareItem.Sensors[(int)SensorType.Temperature].Value);
// // 获取CPU风扇转速
// var fanSpeed = hardwareItem.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Fan);
// if (fanSpeed != null)
// Console.WriteLine("CPU Fan Speed: " + fanSpeed.Value);
// }
// else if (hardwareItem.HardwareType == HardwareType.GpuNvidia || hardwareItem.HardwareType == HardwareType.GpuAti)
// {
// Console.WriteLine("GPU Temperature: " + hardwareItem.Sensors[(int)SensorType.Temperature].Value);
// // 类似地获取GPU风扇转速
// }
// else if (hardwareItem.HardwareType == HardwareType.HDD)
// {
// // 注意:硬盘通常不直接报告温度,这可能需要特定的SMART属性查询
// }
// else if (hardwareItem.HardwareType == HardwareType.Mainboard)
// {
// // 主板温度可能作为传感器之一报告
// Console.WriteLine("Motherboard Temperature: " + hardwareItem.Sensors[(int)SensorType.Temperature].Value);
// }
// }
// // 根据需要决定是否继续循环或延时
// System.Threading.Thread.Sleep(1000); // 例如,每秒检查一次
// }
// }
}
}
06-13