上一篇文章说到了获取计算机物理和操作系统的相关信息的方法(获取windows 操作系统下的硬件或操作系统信息等),这篇文章短一点,将下如何获取CPU的温度。
获取CPU的温度,需要指定ManagementObjectSearcher 的查询范围,参数发生变化。另外需要程序需要使用管理员权限!
public class Temperature { public double CurrentValue { get; set; } public string InstanceName { get; set; } public static List<Temperature> Temperatures { get { List<Temperature> result = new List<Temperature>(); ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"rootWMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"); foreach (ManagementObject obj in searcher.Get()) { double temperature = Convert.ToDouble(obj["CurrentTemperature"].ToString()); temperature = (temperature - 2732) / 10.0; result.Add(new Temperature { CurrentValue = temperature, InstanceName = obj["InstanceName"].ToString() }); } return result; } } public override string ToString() { return $"{InstanceName}: {CurrentValue}℃"; } }