Read System Properties like CPU Temperatures with .NET Core

Read System Properties like CPU Temperatures with .NET Core

Through a forum post I discovered the NuGet package OpenHardwareMonitorLibCore, and wanted to test it.

Apparently this is a wrapper of a very popular tool (https://openhardwaremonitor.org/), with whose help you can read out all kinds of system properties. This includes the CPU and its sensors.

So I took a look at the library and built a short sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;

namespace ConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Read system information..." + Environment.NewLine);
            Console.WriteLine("Results:");
            SystemInfo systemInfo = await ReadSystemInfoAsync();

            foreach (SystemInfo.CoreInfo cInfo in systemInfo.CoreInfos)
            {
                Console.WriteLine($"Name: {cInfo.Name} - {cInfo.Load} % - {cInfo.Temp} �C");
            }

            Console.WriteLine("Done.");
            Console.ReadKey();
        }

        public static async Task<SystemInfo> ReadSystemInfoAsync()
        {
            return await Task.Run(() =>
            {
                SystemInfo systemInfo = new SystemInfo();

                SystemVisitor updateVisitor = new SystemVisitor();
                Computer computer = new Computer();

                try
                {
                    computer.Open();
                    computer.CPUEnabled = true;

                    computer.Accept(updateVisitor);

                    foreach (IHardware hw in computer.Hardware
                        .Where(hw => hw.HardwareType == HardwareType.CPU))
                    {
                        foreach (ISensor sensor in hw.Sensors)
                        {
                            switch (sensor.SensorType)
                            {
                                case SensorType.Load:
                                    systemInfo.AddOrUpdateCoreLoad(
                                    sensor.Name, sensor.Value.GetValueOrDefault(0));

                                    break;
                                case SensorType.Temperature:
                                    systemInfo.AddOrUpdateCoreTemp(
                                    sensor.Name, sensor.Value.GetValueOrDefault(0));

                                    break;
                            }
                        }
                    }
                }
                finally
                {
                    computer.Close();
                }

                return systemInfo;
            });
        }
    }

    public class SystemInfo
    {
        public class CoreInfo
        {
            public string Name { get; set; }
            public double Load { get; set; }
            public double Temp { get; set; }
        }

        public List<CoreInfo> CoreInfos = new List<CoreInfo>();

        private CoreInfo GetCoreInfo(string name)
        {
            CoreInfo coreInfo = CoreInfos.SingleOrDefault(c => c.Name == name);
            if (coreInfo is null)
            {
                coreInfo = new CoreInfo { Name = name };
                CoreInfos.Add(coreInfo);
            }

            return coreInfo;
        }

        public void AddOrUpdateCoreTemp(string name, double temp)
        {
            CoreInfo coreInfo = GetCoreInfo(name);
            coreInfo.Temp = temp;
        }

        public void AddOrUpdateCoreLoad(string name, double load)
        {
            CoreInfo coreInfo = GetCoreInfo(name);
            coreInfo.Load = load;
        }
    }

    public class SystemVisitor : 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) { }
    }
}

This snippet reads all my cores as well as their name, load and temperature.

    Read system information...

    Results:
    Name: CPU Core #1 - 50 % - 0 �C
    Name: CPU Core #2 - 0 % - 0 �C
    Name: CPU Core #3 - 0 % - 0 �C
    Name: CPU Core #4 - 0 % - 0 �C
    Name: CPU Core #5 - 0 % - 0 �C
    Name: CPU Core #6 - 0 % - 0 �C
    Name: CPU Core #7 - 0 % - 0 �C
    Name: CPU Core #8 - 0 % - 0 �C
    Name: CPU Core #9 - 0 % - 0 �C
    Name: CPU Core #10 - 0 % - 0 �C
    Name: CPU Core #12 - 0 % - 0 �C
    Name: CPU Total - 4,17 % - 0 �C
    Name: CPU Package - 0 % - 0 �C
Done.

Unfortunately you can already see that the temperature of the cores is not read out. I couldn't find out why.

Blocking Code

The NuGet package / lib does not offer any asynchronous methods, which means that the system properties have to be read within a Task or Thread. Otherwise, the UI of a Windows Forms or WPF application hangs. In the worst case Windows closes your application.

NET Core and .NET Framework

Unlike the .NET Framework, .NET Core itself does not provide any functionality to read such system information. The library is also not cross-platform capable and works with a .NET Core 3.0 console application only on Windows.