电脑装配网

C# 获取Windows系统的CPU温度

 人阅读 | 作者xiaolin | 时间:2023-10-09 13:20

要在C#中获取Windows系统的CPU温度,您可以使用第三方库,如OpenHardwareMonitor或WMI(Windows Management Instrumentation)。

以下是使用这两种方法的示例代码:

使用OpenHardwareMonitor库:

using OpenHardwareMonitor.Hardware;using System;class Program{ static void Main() { Computer computer = new Computer(); computer.Open(); computer.CPUEnabled = true; foreach (var hardware in computer.Hardware) { if (hardware.HardwareType == HardwareType.CPU) { hardware.Update(); foreach (var sensor in hardware.Sensors) { if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("Core")) { Console.WriteLine(#34;CPU Temperature: {sensor.Value}°C"); } } } } computer.Close(); }}

使用WMI(Windows Management Instrumentation):

using System;using System.Management;class Program{ static void Main() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"); ManagementObjectCollection collection = searcher.Get(); foreach (ManagementObject obj in collection) { double temperature = Convert.ToDouble(obj["CurrentTemperature"]) / 10 - 273.15; Console.WriteLine(#34;CPU Temperature: {temperature}°C"); } }}

请注意,使用OpenHardwareMonitor库需要将其添加为项目的引用。使用WMI不需要额外的引用。

这些示例代码将获取CPU温度并将其打印到控制台。

您可以根据需要进行进一步处理或将其集成到您的应用程序中。

请确保以管理员权限运行您的应用程序,以便访问系统信息。


文章标签:

本文链接:『转载请注明出处』