如何使用带有Open Hardware Monitor DLL的VB.NET查看CPU的温度

时间:2015-08-02 17:32:52

标签: vb.net cpu temperature openhardwaremonitor

我的VB.NET程序中需要CPU的温度传感器,我想使用OpenHardwareMonitorLib.dll来获取CPU temp的值。

我从这里下载dll:http://openhardwaremonitor.org/downloads/

我只有这段代码:

private List<Item> itemList = ///etc.

public List<Item> ItemList //binding to this
{
    public get { return itemList.OrderByDescending(x => x.price).ToList(); }
}

但有了这个我只得到我的硬件的名称我需要CPU温度。

我尝试在VB.NET中使用WMI,但是我收到了Not Supported消息。

4 个答案:

答案 0 :(得分:1)

我刚刚在VS2015中工作,所以我想分享......

1 - 下载openhardware监视器,解压缩并将.dll放入项目的Bin \ debug文件夹中

2-确保以管理员身份启动VS

3-导入OpenHardwareMonitor和OpenHardwarMonitor.Hardware

4-级全局{公共CPUTemp为Double,Public cp为新计算机()}

然后我在后面调整的后台工作程序中使用了以下代码:

'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

答案 1 :(得分:0)

这让我回到了所有cpu传感器的温度。确保使用管理员权限运行Visual Studio实例。否则它可能不起作用。 OpenHardwareMonitor DLL也需要位于本地磁盘上才能工作。

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

答案 2 :(得分:0)

谢谢,我也找到了这段代码并且完美无缺!

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

答案 3 :(得分:0)

检查此实时温度信息系统。

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

检查屏幕截图并附上源代码。 Working Screenshot.

Source Code