2009-07-28 406 views
47

我需要爲我正在開發的應用程序收集一些系統信息。可用內存和CPU負載很容易使用C#。不幸的是,CPU溫度並不那麼容易。我一直在使用WMI嘗試,但使用如何獲得CPU溫度?

Win32_TemperatureProbe 

MSAcpi_ThermalZoneTemperature 

有沒有人已經處理了這個問題,我不能得到什麼嗎?我不知道如何監測方案,SiSoftware Sandra裏,可以得到的信息......

萬一有人有興趣,這裏是類的代碼:

public class SystemInformation 
{ 
    private System.Diagnostics.PerformanceCounter m_memoryCounter; 
    private System.Diagnostics.PerformanceCounter m_CPUCounter; 

    public SystemInformation() 
    { 
     m_memoryCounter = new System.Diagnostics.PerformanceCounter(); 
     m_memoryCounter.CategoryName = "Memory"; 
     m_memoryCounter.CounterName = "Available MBytes"; 

     m_CPUCounter = new System.Diagnostics.PerformanceCounter(); 
     m_CPUCounter.CategoryName = "Processor"; 
     m_CPUCounter.CounterName = "% Processor Time"; 
     m_CPUCounter.InstanceName = "_Total"; 
    } 

    public float GetAvailableMemory() 
    { 
     return m_memoryCounter.NextValue(); 
    } 

    public float GetCPULoad() 
    { 
     return m_CPUCounter.NextValue(); 
    } 

    public float GetCPUTemperature() 
    { 
     //... 
     return 0; 
    } 
} 
+0

如果你願意付出我會去了解一下http://www.cpuid-pro.com如果我沒記錯的相同庫'Z-CPU `使用.. – Peter 2015-01-15 07:49:30

回答

19

我敢肯定它取決於製造商,因爲它們將通過I/O端口訪問。如果您正在嘗試使用特定的電路板,請嘗試查看手冊和/或聯繫製造商。

如果你想爲很多不同的電路板做這個,我建議你與SiSoftware聯繫,或者準備閱讀大量的主板手冊。

另外,不是所有的板都有溫度監視器。

您也可能遇到從內核獲得特權訪問的問題。

3

有一個關於如何做到這一點的C#示例代碼的博客文章here

+1

這仍然要求供應商的驅動程序通過WMI公開它。 – ewanm89 2009-07-28 16:15:35

30

我知道這個帖子是舊的,但只是想添加一條評論,如果有人應該看這個帖子,並試圖找到這個問題的解決方案。

通過使用WMI方法,您確實可以在C#中非常輕鬆地讀取CPU溫度。

爲了得到一個攝氏度值,我創建了一個包裝器,它轉換WMI返回的值並將其包裝成易於使用的對象。

請記住在Visual Studio中添加對System.Management.dll的引用。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Management; 

namespace RCoding.Common.Diagnostics.SystemInfo 
{ 
    public class Temperature 
    { 
     public double CurrentValue { get; set; } 
     public string InstanceName { get; set; } 
     public static List<Temperature> Temperatures 
     { 
      get 
      { 
       List<Temperature> result = new List<Temperature>(); 
       ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"); 
       foreach (ManagementObject obj in searcher.Get()) 
       { 
        Double temp = Convert.ToDouble(obj["CurrentTemperature"].ToString()); 
        temp = (temp - 2732)/10.0; 
        result.Add(new Temperature { CurrentValue = temp, InstanceName = obj["InstanceName"].ToString() }); 
       } 
       return result; 

      } 
     } 
    } 
} 

更新25.06.2010:

(剛剛看到一個鏈接被髮布到同一種解決方案上面...... 無論如何,我會離開這片的代碼,如果有人應該要使用它:-))

+7

你讀過這個問題了嗎? OP表示他已經嘗試過MSAcpi_ThermalZoneTemperature ... – 2011-07-20 16:16:37

+0

這不是CPU溫度,我認爲它是錶殼溫度。您可以使用CPUID進行檢查。 – zezba9000 2017-09-06 22:15:23

47

對於其他人誰可能會通過這裏,也許看看:http://openhardwaremonitor.org/

按照李nk,起初你可能會想,「嘿,那是一個應用程序,這就是它被刪除的原因,問題是如何從C#代碼中做到這一點,而不是找到一個能夠告訴我溫度的應用程序......」它表明你不願意投入足夠的時間閱讀「Open Hardware Monitor」。

,還包括一個數據接口,這裏是說明:

數據接口 開放硬件監控所有傳感器數據發佈到 WMI(Windows管理規範)。這允許其他 應用程序讀取和使用傳感器信息。 A 接口的初步文檔可以找到here(click)

當您下載它時,它包含OpenHardwareMonitor.exe應用程序,您不會尋找那個。它還包含OpenHardwareMonitorLib.dll,你正在尋找那個。

它大部分(如果不是100%的話)只是WinRing0 API的封裝,如果你喜歡它,你可以選擇包裝自己。

我已經從C#應用程序自己嘗試了這一點,它的工作原理。雖然它仍處於測試階段,但它看起來相當穩定。它也是開源的,所以它可能是一個很好的起點。

在一天結束時,我發現很難相信這不是關於這個問題的主題。

1

它可以通過WMI在您的代碼中完成。我找到了一個來自微軟的工具,爲它創建了代碼。

的WMI代碼Creator工具允許您生成的VBScript,C#和VB使用WMI來完成管理任務,如查詢 管理數據,從WMI類執行的方法 .NET代碼,或使用WMI接收 事件通知。

您可以下載它here

1

這取決於您的計算機是否支持WMI。我的電腦也無法運行這個WMI演示程序。

但我通過Open Hardware Monitor成功獲取了CPU溫度。在Visual Studio中添加Openhardwaremonitor參考。這很容易。試試這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using OpenHardwareMonitor.Hardware; 
namespace Get_CPU_Temp5 
{ 
    class Program 
    { 
     public class UpdateVisitor : IVisitor 
     { 
      public void VisitComputer(IComputer computer) 
      { 
       computer.Traverse(this); 
      } 
      public void VisitHardware(IHardware hardware) 
      { 
       hardware.Update(); 
       foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this); 
      } 
      public void VisitSensor(ISensor sensor) { } 
      public void VisitParameter(IParameter parameter) { } 
     } 
     static void GetSystemInfo() 
     { 
      UpdateVisitor updateVisitor = new UpdateVisitor(); 
      Computer computer = new Computer(); 
      computer.Open(); 
      computer.CPUEnabled = true; 
      computer.Accept(updateVisitor); 
      for (int i = 0; i < computer.Hardware.Length; i++) 
      { 
       if (computer.Hardware[i].HardwareType == HardwareType.CPU) 
       { 
        for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++) 
        { 
         if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature) 
           Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r"); 
        } 
       } 
      } 
      computer.Close(); 
     } 
     static void Main(string[] args) 
     { 
      while (true) 
      { 
       GetSystemInfo(); 
      } 
     } 
    } 
} 

您需要以管理員身份運行此演示。

你可以看到這裏的教程: http://www.lattepanda.com/topic-f11t3004.html