C# CPU and GPU Temp

南笙酒味 提交于 2019-12-11 04:05:03

问题


I'm in the process of creating a personal monitoring program for system performance, and I'm having issues figuring out how C# retrieves CPU and GPU Temperature information.

I already have the program retrieve the CPU Load and Frequency information(as well as various other things) through PerformanceCounter, but I haven't been able to find the Instance, Object,and Counter variables for CPU temp.

Also, I need to be able to get the temperature of more than one GPU, as I have two.

What do I do?


回答1:


You can use the WMI for that, there is a c# code generator for WMI that helps a lot when creating WMI quires as it is not documented that well.

The WMI code generator can be found here: http://www.microsoft.com/en-us/download/details.aspx?id=8572

a quick try generates something like this:

  public static void Main()
    {
        try
        {
            ManagementObjectSearcher searcher = 
                new ManagementObjectSearcher("root\\WMI", 
                "SELECT * FROM MSAcpi_ThermalZoneTemperature"); 

                          foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("MSAcpi_ThermalZoneTemperature instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("CurrentTemperature: {0}", queryObj["CurrentTemperature"]);
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }
    }

This may not be exactly what you need just try around with the properties and classes available




回答2:


Use the MSAcpi_ThermalZoneTemperature class in WMI.




回答3:


You can get the CPU temp in both WMI and Openhardwaremonitor way.

Open Hardwaremonitor:

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();
           }
       }
   }
}

WMI:

using System;
using System.Diagnostics;
using System.Management;
class Program
{
   static void Main(string[] args)
   {
       Double CPUtprt = 0;
       System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(@"root\WMI", "Select * From MSAcpi_ThermalZoneTemperature");
       foreach (System.Management.ManagementObject mo in mos.Get())
       {
           CPUtprt = Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10;
          Console.WriteLine("CPU temp : " + CPUtprt.ToString() + " °C");
       }
   }
}

I found a nice tutorial here, I get the CPU temp successfully.

http://www.lattepanda.com/topic-f11t3004.html




回答4:


// Gets temperature info from OS and prints it to the console
PerformanceCounter perfCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
PerformanceCounter tempCount = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM");
while (true)
{
  Console.WriteLine("Processor time: " + perfCount.NextValue() + "%");
  // -273.15 is the conversion from degrees Kelvin to degrees Celsius
  Console.WriteLine("Temperature: {0} \u00B0C", (tempCount.NextValue() - 273.15f));
  Thread.Sleep(1000);
}


来源:https://stackoverflow.com/questions/29607595/c-sharp-cpu-and-gpu-temp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!