Mile M.
Mile M.

Reputation: 45

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

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.

Upvotes: 1

Views: 12925

Answers (4)

Sagar Maher
Sagar Maher

Reputation: 46

Check This real time Temperature Info system.

Imports System
Imports System.Management
Imports OpenHardwareMonitor
Imports OpenHardwareMonitor.Hardware

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Timer1.Enabled = True
End Sub

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

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    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)

        Label1.Text = tempSensors.ToList.Item(0).Value
        Label2.Text = tempSensors.ToList.Item(1).Value
        MetroProgressSpinner1.Value = Label1.Text
        MetroProgressSpinner2.Value = Label2.Text
    End If
End Sub

End Class

Check Screenshot and attach source code. Working Screenshot.

Source Code

Upvotes: 1

Sk3letron
Sk3letron

Reputation: 21

I just got this working in VS2015 so figured I would share...

1 - Download openhardware monitor, extract and put the .dll into your Bin\debug folder for your project

2- Make sure you start VS as an administrator

3- Import OpenHardwareMonitor and OpenHardwarMonitor.Hardware

4- Class Level Globals {Public CPUTemp as Double, Public cp As New Computer()}

I then used the following code in a background worker which I tweaked from above:

'GET CPU TEMPERATURE
        Dim cpu = cp.Hardware.Where(Function(h) h.HardwareType = HardwareType.CPU).FirstOrDefault()
        cpu.Update()
        For i As Integer = 0 To cp.Hardware.Count() - 1
            Dim hw = cp.Hardware(i)
            Select Case hw.HardwareType
                Case HardwareType.CPU

                    Dim sensor = hw.Sensors(5) 'AVERAGE CPU TEMPERATURE
                    CPUTemp = sensor.Value

            End Select
        Next

The temperature displays in Celsius as shown in my little widget

Upvotes: 2

Mile M.
Mile M.

Reputation: 45

Thanks, I also found this code and perfectly working!

Imports OpenHardwareMonitor
Imports OpenHardwareMonitor.Hardware

Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal 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
            Dim hw = cp.Hardware(i)

            Select Case hw.HardwareType
                Case HardwareType.Mainboard
                    TextBox3.AppendText("Motherboard" & vbCrLf)
                    For k = 0 To hw.SubHardware.Count - 1
                        Dim subhardware = hw.SubHardware(k)
                        TextBox3.AppendText(subhardware.Name & vbCrLf)
                        For j = 0 To subhardware.Sensors.Count - 1
                            Dim sensor = subhardware.Sensors(j)
                            TextBox3.AppendText(sensor.SensorType & " - " & sensor.Name & " - " & sensor.Value & vbCrLf)
                        Next
                    Next
                Case HardwareType.CPU
                    TextBox3.AppendText("CPU" & vbCrLf)
                    For j = 0 To hw.Sensors.Count - 1
                        Dim sensor = hw.Sensors(j)
                        TextBox3.AppendText(sensor.SensorType & " - " & sensor.Name & " - " & sensor.Value & vbCrLf)
                    Next
                Case HardwareType.RAM
                    TextBox3.AppendText("RAM" & vbCrLf)
                    For j = 0 To hw.Sensors.Count - 1
                        Dim sensor = hw.Sensors(j)
                        TextBox3.AppendText(sensor.SensorType & " - " & sensor.Name & " - " & sensor.Value & vbCrLf)
                    Next
            End Select
        Next
    End Sub

End Class

Upvotes: 1

Saragis
Saragis

Reputation: 1792

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()

Upvotes: 2

Related Questions