VB.Net 使用 WMI 取得 CPU 溫度

VB.Net 使用 WMI 取得 CPU 溫度

VB.Net 使用 WMI ( Windows Management Instrumentation ) 來取得 CPU 溫度

<< VB.Net >>

請先加入參考 System.Management

Imports System

Imports System.Management

Public Class Form1

Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click

Dim CPUtprt As Integer = CPU_Temperature()

If CPUtprt >= 100 Then

MessageBox.Show("CPU 溫度: " & CPUtprt.ToString & " °C" & ControlChars.CrLf & _

"溫度破錶嚕! 快準備煎蛋囉! " & ControlChars.CrLf & _

"或開啟自動灑水系統降溫喔!")

Else

MessageBox.Show("CPU 溫度: " & CPU_Temperature() & " °C")

End If

End Sub

' 取得CPU 溫度

Private Function CPU_Temperature() As Integer

Dim Q As String = "Select * From MSAcpi_ThermalZoneTemperature"

Dim mos As New ManagementObjectSearcher("root\WMI", Q)

For Each mo As ManagementObject In mos.Get

Return Convert.ToInt32(mo.GetPropertyValue("CurrentTemperature") - 2732) / 10

Next

End Function

End Class

CPU Temperature

' ================================================================

<< VB6 >>

Private Sub Command1_Click()

MsgBox "CPU 溫度 : " & CPU_Temperature & " °C"

End Sub

Private Function CPU_Temperature() As Integer

Dim WMIsvc As Object

Dim CltItems As Object, CltItem As Object

Dim Q As String

Set WMIsvc = GetObject("winmgmts:\\.\root\WMI")

Q = "SELECT * FROM MSAcpi_ThermalZoneTemperature"

Set CltItems = WMIsvc.ExecQuery(Q, , 48)

For Each CltItem In CltItems

CPU_Temperature = CltItem.CurrentTemperature

Next

CPU_Temperature = (CPU_Temperature - 2732) / 10

Set CltItem = Nothing

Set CltItems = Nothing

Set WMIsvc = Nothing

End Function