none
求CPU温度 RRS feed

答案

  • Hi 

    你可以使用openhardwareMonitorLib.dll

    1.添加引用OpenHardwareMonitorLib.dll

    2.添加Imports OpenHardwareMonitor.Hardware

    代码:

    Imports OpenHardwareMonitor.Hardware
    
    Public Class Form1
        Public Class UpdateVisitor
            Implements OpenHardwareMonitor.Hardware.IVisitor
            Private Sub VisitComputer(computer As IComputer) Implements IVisitor.VisitComputer
                computer.Traverse(Me)
            End Sub
    
            Private Sub VisitHardware(hardware As IHardware) Implements IVisitor.VisitHardware
                hardware.Update()
    
                For Each subHardware As IHardware In hardware.SubHardware
                    subHardware.Accept(Me)
                Next
            End Sub
    
            Private Sub IVisitor_VisitSensor(sensor As ISensor) Implements IVisitor.VisitSensor
                Throw New NotImplementedException()
            End Sub
    
            Private Sub IVisitor_VisitParameter(parameter As IParameter) Implements IVisitor.VisitParameter
                Throw New NotImplementedException()
            End Sub
        End Class
    
        Private updateVisit As UpdateVisitor = New UpdateVisitor()
        Private computer As Computer
        Private TemperatureSensors As List(Of ISensor) = New List(Of ISensor)()
        Private FrequenceSensors As List(Of ISensor) = New List(Of ISensor)()
        Private LVIs As List(Of ListViewItem) = New List(Of ListViewItem)()
        Private CurTemps As Single()
        Private CurFreqs As Single()
        Private TempCount As Integer = 0
        Private FreqCount As Integer = 0
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InitComputer()
            InitHardware()
        End Sub
        Private Sub InitComputer()
            Try
                computer = New Computer()
                computer.Open()
                computer.CPUEnabled = True
            Catch
                MessageBox.Show("error")
            End Try
        End Sub
    
        Private Sub InitHardware()
            Try
                computer.Accept(updateVisit)
    
                For i As Integer = 0 To computer.Hardware.Length - 1
    
                    If computer.Hardware(i).HardwareType = HardwareType.CPU Then
    
                        For j As Integer = 0 To computer.Hardware(i).Sensors.Length - 1
    
                            If computer.Hardware(i).Sensors(j).SensorType = SensorType.Temperature Then
                                TemperatureSensors.Add(computer.Hardware(i).Sensors(j))
                                Dim lvi As ListViewItem = New ListViewItem()
                                lvi.SubItems(0).Text = computer.Hardware(i).Sensors(j).Name
                                lvi.SubItems.Add(computer.Hardware(i).Sensors(j).Value.ToString() & " ¡ãC")
                                lvi.SubItems.Add("")
                                lvi.SubItems.Add("N/A")
                                LVIs.Add(lvi)
                            ElseIf computer.Hardware(i).Sensors(j).SensorType = SensorType.Clock Then
    
                                If computer.Hardware(i).Sensors(j).Name.Contains("CPU Core") Then
                                    FrequenceSensors.Add(computer.Hardware(i).Sensors(j))
                                End If
                            End If
                        Next
                    End If
                Next
    
                TempCount = TemperatureSensors.Count
                FreqCount = FrequenceSensors.Count
                CurTemps = New Single(TempCount - 1) {}
                CurFreqs = New Single(FreqCount - 1) {}
    
                For i As Integer = 0 To TempCount - 1
    
                    If i < FreqCount Then
                        Dim freq As Single = CSng(FrequenceSensors(i).Value)
                        LVIs(i).SubItems(2).Text = freq.ToString("F0") & " MHz"
                    Else
                        LVIs(i).SubItems(2).Text = "N/A"
                    End If
                Next
    
                For Each lvi As ListViewItem In LVIs
                    LV_CPU.Items.Add(lvi)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Try
                computer.Accept(updateVisit)
    
                For i As Integer = 0 To TemperatureSensors.Count - 1
                    LVIs(i).SubItems(1).Text = TemperatureSensors(i).Value.ToString() & " and"
                    CurTemps(i) = CSng(TemperatureSensors(i).Value)
                Next
    
                For i As Integer = 0 To FrequenceSensors.Count - 1
                    Dim freq As Single = CSng(FrequenceSensors(i).Value)
                    freq = CSng(Math.Round(freq))
                    LVIs(i).SubItems(2).Text = freq.ToString("F0") & " MHz"
                    CurFreqs(i) = freq
                Next
    
            Catch
            End Try
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click &amp;amp;amp;quot;Mark as Answer&amp;amp;amp;quot; the responses that resolved your issue, and to click &amp;amp;amp;quot;Unmark as Answer&amp;amp;amp;quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 金灶沐 2018年7月10日 4:46
    2018年7月10日 2:20

全部回复

  • Hi 

    你可以使用openhardwareMonitorLib.dll

    1.添加引用OpenHardwareMonitorLib.dll

    2.添加Imports OpenHardwareMonitor.Hardware

    代码:

    Imports OpenHardwareMonitor.Hardware
    
    Public Class Form1
        Public Class UpdateVisitor
            Implements OpenHardwareMonitor.Hardware.IVisitor
            Private Sub VisitComputer(computer As IComputer) Implements IVisitor.VisitComputer
                computer.Traverse(Me)
            End Sub
    
            Private Sub VisitHardware(hardware As IHardware) Implements IVisitor.VisitHardware
                hardware.Update()
    
                For Each subHardware As IHardware In hardware.SubHardware
                    subHardware.Accept(Me)
                Next
            End Sub
    
            Private Sub IVisitor_VisitSensor(sensor As ISensor) Implements IVisitor.VisitSensor
                Throw New NotImplementedException()
            End Sub
    
            Private Sub IVisitor_VisitParameter(parameter As IParameter) Implements IVisitor.VisitParameter
                Throw New NotImplementedException()
            End Sub
        End Class
    
        Private updateVisit As UpdateVisitor = New UpdateVisitor()
        Private computer As Computer
        Private TemperatureSensors As List(Of ISensor) = New List(Of ISensor)()
        Private FrequenceSensors As List(Of ISensor) = New List(Of ISensor)()
        Private LVIs As List(Of ListViewItem) = New List(Of ListViewItem)()
        Private CurTemps As Single()
        Private CurFreqs As Single()
        Private TempCount As Integer = 0
        Private FreqCount As Integer = 0
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InitComputer()
            InitHardware()
        End Sub
        Private Sub InitComputer()
            Try
                computer = New Computer()
                computer.Open()
                computer.CPUEnabled = True
            Catch
                MessageBox.Show("error")
            End Try
        End Sub
    
        Private Sub InitHardware()
            Try
                computer.Accept(updateVisit)
    
                For i As Integer = 0 To computer.Hardware.Length - 1
    
                    If computer.Hardware(i).HardwareType = HardwareType.CPU Then
    
                        For j As Integer = 0 To computer.Hardware(i).Sensors.Length - 1
    
                            If computer.Hardware(i).Sensors(j).SensorType = SensorType.Temperature Then
                                TemperatureSensors.Add(computer.Hardware(i).Sensors(j))
                                Dim lvi As ListViewItem = New ListViewItem()
                                lvi.SubItems(0).Text = computer.Hardware(i).Sensors(j).Name
                                lvi.SubItems.Add(computer.Hardware(i).Sensors(j).Value.ToString() & " ¡ãC")
                                lvi.SubItems.Add("")
                                lvi.SubItems.Add("N/A")
                                LVIs.Add(lvi)
                            ElseIf computer.Hardware(i).Sensors(j).SensorType = SensorType.Clock Then
    
                                If computer.Hardware(i).Sensors(j).Name.Contains("CPU Core") Then
                                    FrequenceSensors.Add(computer.Hardware(i).Sensors(j))
                                End If
                            End If
                        Next
                    End If
                Next
    
                TempCount = TemperatureSensors.Count
                FreqCount = FrequenceSensors.Count
                CurTemps = New Single(TempCount - 1) {}
                CurFreqs = New Single(FreqCount - 1) {}
    
                For i As Integer = 0 To TempCount - 1
    
                    If i < FreqCount Then
                        Dim freq As Single = CSng(FrequenceSensors(i).Value)
                        LVIs(i).SubItems(2).Text = freq.ToString("F0") & " MHz"
                    Else
                        LVIs(i).SubItems(2).Text = "N/A"
                    End If
                Next
    
                For Each lvi As ListViewItem In LVIs
                    LV_CPU.Items.Add(lvi)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Try
                computer.Accept(updateVisit)
    
                For i As Integer = 0 To TemperatureSensors.Count - 1
                    LVIs(i).SubItems(1).Text = TemperatureSensors(i).Value.ToString() & " and"
                    CurTemps(i) = CSng(TemperatureSensors(i).Value)
                Next
    
                For i As Integer = 0 To FrequenceSensors.Count - 1
                    Dim freq As Single = CSng(FrequenceSensors(i).Value)
                    freq = CSng(Math.Round(freq))
                    LVIs(i).SubItems(2).Text = freq.ToString("F0") & " MHz"
                    CurFreqs(i) = freq
                Next
    
            Catch
            End Try
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click &amp;amp;amp;quot;Mark as Answer&amp;amp;amp;quot; the responses that resolved your issue, and to click &amp;amp;amp;quot;Unmark as Answer&amp;amp;amp;quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 金灶沐 2018年7月10日 4:46
    2018年7月10日 2:20
  • 谢谢,可以使用
    2018年7月10日 4:47