強力鎯頭 VB BLOG

文章分類

每月文章

文章標籤

全部標籤

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

 

posted on 2008/3/24 21:42 | 1 人推薦 我要推薦 | 閱讀數 : 1569 | 文章分類 [ VB / VBA VB.Net / C#.Net / WMI / ADSI / WSH ] 訂閱

Feedback

# re: VB.Net 使用 WMI 取得 CPU 溫度 2008/10/21 下午 10:30 jason 回覆

<< VB6 >>
我執行 For Each CltItem In CltItems
CPU_Temperature =CltItem.CurrentTemperature
Next
這行會發生問題
我是哪裡出錯全照上面複製貼上,我是哪沒做好




VB超新手 = =

# re: VB.Net 使用 WMI 取得 CPU 溫度 2008/11/12 下午 10:45 C# 回覆

For Each CltItem In CltItems未指定的错误

標題
姓名
電子郵件 (將不會被顯示)
個人網頁
內容
登入後使用進階評論
Please add 7 and 3 and type the answer here: