Learn how to retrieve the CPU's temperature with C# in your WinForms application.

Although not every motherboard disposes of a temperature monitor, you may be able to retrieve this information if available from the motherboard through the WMI class of Windows, querying specifically the MSAcpi_ThermalZoneTemperature class. The Win32_TemperatureProbe WMI class represents the properties of a temperature sensor (electronic thermometer). Most of the information that the Win32_TemperatureProbe WMI class provides comes from SMBIOS. Real-time readings for the CurrentReading property cannot be extracted from SMBIOS tables.

In this article, we'll share with you a pretty simple way to retrieve the current temperature of your CPU with C# through the WMI class of Windows.

1. Add reference to the System.Management class

In order to obtain information about the motherboard with C# in WinForms, you will need to have access to the System Management class:

using System.Management;

However, in some Visual Studio versions (specifically in 2010 and later) you'll need to add the reference (.DLL) in your project manually. To do that follow these steps:

  1. Right Click on Project, Add References

  2. Select the Assemblies (framework) Tab and Search for System.Management and finally add the reference and click OK.

Add System.Management reference visual studio manually

We need to add the System.Management to create queries in WMI Classes. Read more about retrieving WMI Classes in .NET in msdn here.

By the other side, remember that all the implementations that uses the ManagementObjectSearcher class to obtain system information, whose properties values are integer values (0-100) and those values aren't related to the property name (i.e with the Video_Controller GPU class in the Architecture property that returns a value between 0 and 9) and you expect a very specific value (like x86 or x64), then is probably that you're passing by some information! Please read the documentation of the class in the Microsoft developer network website (providen in every part of the article respectively) to get a detailed description of every property.

2. Retrieving CPU temperature and instance name

Now that you have access to the System Management class, you will be able to query the mentioned class to obtain the temperature of the CPU with the following code:

Note

You might run into problems getting privileged access from the kernel, so be sure to run your application or Visual Studio (if you are trying) as administrator.

// Important: don't forget to include the System Management class in your code
using System.Management;

// Create tmp variables to store values during the query
Double temperature = 0;
String instanceName = "";


// Query the MSAcpi_ThermalZoneTemperature API
// Note: run your app or Visual Studio (while programming) or you will get "Access Denied"
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature");

foreach (ManagementObject obj in searcher.Get())
{
    temperature = Convert.ToDouble(obj["CurrentTemperature"].ToString());
    // Convert the value to celsius degrees
    temperature = (temperature - 2732) / 10.0;
    instanceName = obj["InstanceName"].ToString();
}

// Print the values e.g:

// 29.8
Console.WriteLine(temperature);

// ACPI\ThermalZone\TZ01_0
Console.WriteLine(instanceName);

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors