我在我的VB.NET程序中需要CPU的温度传感器,我想使用OpenHardwareMonitorLib.dll来获取CPU temp的值。
我从这里下载dll:http://openhardwaremonitor.org/downloads/
我只有这个代码:
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
但有了这个我只得到我的硬件名称我需要CPU温度。
我尝试在VB.NET中使用WMI,但是我得到了Not Supported消息。
我刚刚在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
这让我回到了所有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()
谢谢,我也找到了这个代码并且完美地工作!
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
检查此实时温度信息系统。
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.