How can I see temperature of CPU using VB.NET with Open Hardware Monitor DLL

Mile M. picture Mile M. · Aug 2, 2015 · Viewed 10.6k times · Source

I need temperature sensor of CPU in my VB.NET program, I want to use OpenHardwareMonitorLib.dll in to take values of CPU temp.

I download dll from here: http://openhardwaremonitor.org/downloads/

I have only this code:

 Imports OpenHardwareMonitor
Imports OpenHardwareMonitor.Hardware

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cp As New Computer()
        cp.Open()
        cp.HDDEnabled = True
        cp.FanControllerEnabled = True
        cp.RAMEnabled = True
        cp.GPUEnabled = True
        cp.MainboardEnabled = True
        cp.CPUEnabled = True

        Dim Info As String = ""
        For i As Integer = 0 To cp.Hardware.Count() - 1


            If cp.Hardware(i).HardwareType = HardwareType.Mainboard Then
                Info += " Motherboard: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
            If cp.Hardware(i).HardwareType = HardwareType.CPU Then
                Info += " Processor: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
            If cp.Hardware(i).HardwareType = HardwareType.GpuNvidia Then
                Info += " Video Card: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
            If cp.Hardware(i).HardwareType = HardwareType.RAM Then
                Info += " RAM: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
            If cp.Hardware(i).HardwareType = HardwareType.HDD Then
                Info += " HDD: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
            If cp.Hardware(i).HardwareType = HardwareType.SuperIO Then
                Info += " SuperIO: " & Trim(cp.Hardware(i).Name) & vbCrLf
            End If
        Next
        TextBox1.Text = Info

    End Sub

End Class

But with this I only get name of my Hardware I need CPU Temperature.

I tried WMI to use in VB.NET but I get Not Supported message.

Answer

Saragis picture Saragis · Aug 2, 2015

This gave me back the temperatures of all my cpu-sensors. Make sure to run the Visual Studio instance with administrator-rights. It might not work otherwise. The OpenHardwareMonitor DLL also needed to be on the local disk for this to work.

    Dim computer As New Computer()
    computer.Open()
    computer.CPUEnabled = True

    Dim cpu = computer.Hardware.Where(Function(h) h.HardwareType = HardwareType.CPU).FirstOrDefault()

    If cpu IsNot Nothing Then
        cpu.Update()

        Dim tempSensors = cpu.Sensors.Where(Function(s) s.SensorType = SensorType.Temperature)
        tempSensors.ToList.ForEach(Sub(s) Console.WriteLine(s.Value))
    End If

    Console.ReadLine()