CPU temperature monitoring

后端 未结 3 1683
小鲜肉
小鲜肉 2020-12-01 06:49

For a programming project I would like to access the temperature readings from my CPU and GPUs. I will be using C#. From various forums I get the impression that there is sp

相关标签:
3条回答
  • 2020-12-01 07:07

    The best way to go for hardware related coding on windows is by using WMI which is a Code Creator tool from Microsoft, the tool will create the code for you based on what you are looking for in hardware related data and what .Net language you want to use.

    The supported langauges currently are: C#, Visual Basic, VB Script.

    0 讨论(0)
  • 2020-12-01 07:13

    For at least the CPU side of things, you could use WMI.

    The namespace\object is root\WMI, MSAcpi_ThermalZoneTemperature

    Sample Code:

    ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher("root\\WMI",
                                     "SELECT * FROM MSAcpi_ThermalZoneTemperature");
    
    ManagementObjectCollection collection = 
        searcher.Get();
    
    foreach(ManagementBaseObject tempObject in collection)
    {
        Console.WriteLine(tempObject["CurrentTemperature"].ToString());
    }
    

    That will give you the temperature in a raw format. You have to convert from there:

    kelvin = raw / 10;
    
    celsius = (raw / 10) - 273.15;
    
    fahrenheit = ((raw / 10) - 273.15) * 9 / 5 + 32;
    
    0 讨论(0)
  • 2020-12-01 07:22

    Note that MSAcpi_ThermalZoneTemperature does not give you the temperature of the CPU but rather the temperature of the motherboard. Also, note that most motherboards do not implement this via WMI.

    You can give the Open Hardware Monitor a go, although it lacks support for the latest processors.

    internal sealed class CpuTemperatureReader : IDisposable
    {
        private readonly Computer _computer;
    
        public CpuTemperatureReader()
        {
            _computer = new Computer { CPUEnabled = true };
            _computer.Open();
        }
    
        public IReadOnlyDictionary<string, float> GetTemperaturesInCelsius()
        {
            var coreAndTemperature = new Dictionary<string, float>();
    
            foreach (var hardware in _computer.Hardware)
            {
                hardware.Update(); //use hardware.Name to get CPU model
                foreach (var sensor in hardware.Sensors)
                {
                    if (sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue)
                        coreAndTemperature.Add(sensor.Name, sensor.Value.Value);
                }
            }
    
            return coreAndTemperature;
        }
    
        public void Dispose()
        {
            try
            {
                _computer.Close();
            }
            catch (Exception)
            {
                //ignore closing errors
            }
        }
    }
    

    Download the zip from the official source, extract and add a reference to OpenHardwareMonitorLib.dll in your project.

    0 讨论(0)
提交回复
热议问题