如何使用带有 Open Hardware Monitor DLL 的 VB.NET 查看 CPU 的温度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31774626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see temperature of CPU using VB.NET with Open Hardware Monitor DLL
提问by Mile M.
I need temperature sensor of CPU in my VB.NET program, I want to use OpenHardwareMonitorLib.dll in to take values of CPU temp.
我的 VB.NET 程序中需要 CPU 的温度传感器,我想在其中使用 OpenHardwareMonitorLib.dll 来获取 CPU 温度的值。
I download dll from here: http://openhardwaremonitor.org/downloads/
我从这里下载 dll: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.
但是有了这个,我只能得到我需要 CPU 温度的硬件的名称。
I tried WMI to use in VB.NET but I get Not Supported message.
我尝试在 VB.NET 中使用 WMI,但收到 Not Supported 消息。
回答by Saragis
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 OpenHardwareMonitorDLL also needed to be on the local disk for this to work.
这给了我所有 CPU 传感器的温度。确保以管理员权限运行 Visual Studio 实例。否则它可能不起作用。该OpenHardwareMonitorDLL还需要在本地磁盘这个工作的。
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()
回答by Mile M.
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
回答by Sk3letron
I just got this working in VS2015 so figured I would share...
我刚刚在 VS2015 中得到了这个,所以我想我会分享......
1 - Download openhardware monitor, extract and put the .dll into your Bin\debug folder for your project
1 - 下载 openhardware 监视器,解压并将 .dll 放入您的项目的 Bin\debug 文件夹中
2- Make sure you start VS as an administrator
2-确保您以管理员身份启动VS
3- Import OpenHardwareMonitor and OpenHardwarMonitor.Hardware
3- 导入 OpenHardwareMonitor 和 OpenHardwarMonitor.Hardware
4- Class Level Globals {Public CPUTemp as Double, Public cp As New Computer()}
4- 类级别全局变量 {公共 CPUTemp 为双,公共 cp 作为新计算机()}
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
回答by Sagar Maher
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.
检查屏幕截图并附上源代码。 工作截图。

