How to Access the CPU Temperature in VB

cpu 1 image by Robert Molnar from Fotolia.com

If you are building a simple hardware statistics program, the first place to look in is the Windows Management Instrumentation (WMI) reference, where you will find a lot of neat queries you can make to the interface to find out many things about your system. You can find the temperature of your CPUs by sending a query to the WMI for "MSAcpi_ThermalZoneTemperature" and seeking to the "CurrentTemperature" value. This is easier said than done, though, so make sure to touch up on your programming skills.

Open up a Visual Basic Windows Form project and call it anything you'd like.

Create a button by selecting the button item on your toolbox and dragging a box across your form covering the area you would like the button to appear in.

Double-click the button you just created to take yourself to its click event function in code view.

Declare a new Management Object Searcher inside the click event and pass its construction parameters as the root WMI class with a query for "MSAcpi_ThermalZoneTemperature." You can do it like this:

Dim s As New System.Management.ManagementObjectSearcher( _ "root/WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature")

Iterate through the searcher you created and make a temporary value within the iteration to display the value. The temperature of the CPU is going to be displayed in a number of 0.1 units of Kelvin. For example, if the CPU temperature is 27455, this can be interpreted as 2745.5 degrees Kelvin. You will have to convert the temperature to the unit you want to display. The following example does the iteration and displays the CPU temperature in Celsius:

For Each q As ManagementObject In s.Get() Dim tmp As Double = CDbl(q("CurrentTemperature") tmp = (tmp -- 2732) / 10.0 MessageBox.Show(tmp.ToString) Next

Most recent