CPU temperature monitoring

后端 未结 3 1703
小鲜肉
小鲜肉 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: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 GetTemperaturesInCelsius()
        {
            var coreAndTemperature = new Dictionary();
    
            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.

提交回复
热议问题