How to get CPU temperature in C#
By FoxLearn 12/12/2024 1:17:28 AM 561
This method is more native to Windows but may not always provide CPU temperature information depending on the system and hardware support.
Although not every motherboard includes a built-in temperature monitor, you can potentially retrieve this information if it's available through Windows Management Instrumentation (WMI). Specifically, you can query the MSAcpi_ThermalZoneTemperature class, which is commonly used for accessing CPU temperature data on some systems.
Additionally, the Win32_TemperatureProbe
WMI class provides properties related to temperature sensors, which are essentially electronic thermometers. Most of the information provided by Win32_TemperatureProbe
comes from the System Management BIOS (SMBIOS). However, real-time temperature readings for the CurrentReading
property cannot be directly extracted from SMBIOS tables.
ManagementObjectSearcher C# Example
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"); ManagementObjectCollection objCollection = searcher.Get(); foreach (ManagementObject obj in objCollection) { double temperature = Convert.ToDouble(obj["CurrentTemperature"].ToString()); temperature = (temperature - 2732) / 10.0; // Convert to Celsius Console.WriteLine($"CPU Temperature: {temperature}°C"); }
This example queries the MSAcpi_ThermalZoneTemperature
class in WMI, which is commonly used to retrieve CPU temperature information on some systems. It converts the temperature from Kelvin to Celsius and prints it.
- How to fix 'Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on'
- How to use BlockingCollection in C#
- Calculating the Distance Between Two Coordinates in C#
- Could Not Find an Implementation of the Query Pattern
- Fixing Invalid Parameter Type in Attribute Constructor
- Objects added to a BindingSource’s list must all be of the same type
- How to use dictionary with tuples in C#
- How to convert a dictionary to a list in C#