2009-07-28 290 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

相关问题