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:

  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Threading.Tasks;
  5using OpenHardwareMonitor.Hardware;
  6
  7namespace ConsoleApp
  8{
  9    class Program
 10    {
 11        static async Task Main(string[] args)
 12        {
 13            Console.WriteLine("Read system information..." + Environment.NewLine);
 14            Console.WriteLine("Results:");
 15            SystemInfo systemInfo = await ReadSystemInfoAsync();
 16
 17            foreach (SystemInfo.CoreInfo cInfo in systemInfo.CoreInfos)
 18            {
 19                Console.WriteLine($"Name: {cInfo.Name} - {cInfo.Load} % - {cInfo.Temp} �C");
 20            }
 21
 22            Console.WriteLine("Done.");
 23            Console.ReadKey();
 24        }
 25
 26        public static async Task<SystemInfo> ReadSystemInfoAsync()
 27        {
 28            return await Task.Run(() =>
 29            {
 30                SystemInfo systemInfo = new SystemInfo();
 31
 32                SystemVisitor updateVisitor = new SystemVisitor();
 33                Computer computer = new Computer();
 34
 35                try
 36                {
 37                    computer.Open();
 38                    computer.CPUEnabled = true;
 39
 40                    computer.Accept(updateVisitor);
 41
 42                    foreach (IHardware hw in computer.Hardware
 43                        .Where(hw => hw.HardwareType == HardwareType.CPU))
 44                    {
 45                        foreach (ISensor sensor in hw.Sensors)
 46                        {
 47                            switch (sensor.SensorType)
 48                            {
 49                                case SensorType.Load:
 50                                    systemInfo.AddOrUpdateCoreLoad(
 51                                    sensor.Name, sensor.Value.GetValueOrDefault(0));
 52
 53                                    break;
 54                                case SensorType.Temperature:
 55                                    systemInfo.AddOrUpdateCoreTemp(
 56                                    sensor.Name, sensor.Value.GetValueOrDefault(0));
 57
 58                                    break;
 59                            }
 60                        }
 61                    }
 62                }
 63                finally
 64                {
 65                    computer.Close();
 66                }
 67
 68                return systemInfo;
 69            });
 70        }
 71    }
 72
 73    public class SystemInfo
 74    {
 75        public class CoreInfo
 76        {
 77            public string Name { get; set; }
 78            public double Load { get; set; }
 79            public double Temp { get; set; }
 80        }
 81
 82        public List<CoreInfo> CoreInfos = new List<CoreInfo>();
 83
 84        private CoreInfo GetCoreInfo(string name)
 85        {
 86            CoreInfo coreInfo = CoreInfos.SingleOrDefault(c => c.Name == name);
 87            if (coreInfo is null)
 88            {
 89                coreInfo = new CoreInfo { Name = name };
 90                CoreInfos.Add(coreInfo);
 91            }
 92
 93            return coreInfo;
 94        }
 95
 96        public void AddOrUpdateCoreTemp(string name, double temp)
 97        {
 98            CoreInfo coreInfo = GetCoreInfo(name);
 99            coreInfo.Temp = temp;
100        }
101
102        public void AddOrUpdateCoreLoad(string name, double load)
103        {
104            CoreInfo coreInfo = GetCoreInfo(name);
105            coreInfo.Load = load;
106        }
107    }
108
109    public class SystemVisitor : IVisitor
110    {
111        public void VisitComputer(IComputer computer) { computer.Traverse(this); }
112
113        public void VisitHardware(IHardware hardware)
114        {
115            hardware.Update();
116            foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
117        }
118
119        public void VisitSensor(ISensor sensor) { }
120        public void VisitParameter(IParameter parameter) { }
121    }
122}

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

 1    Read system information...
 2
 3    Results:
 4    Name: CPU Core #1 - 50 % - 0 �C
 5    Name: CPU Core #2 - 0 % - 0 �C
 6    Name: CPU Core #3 - 0 % - 0 �C
 7    Name: CPU Core #4 - 0 % - 0 �C
 8    Name: CPU Core #5 - 0 % - 0 �C
 9    Name: CPU Core #6 - 0 % - 0 �C
10    Name: CPU Core #7 - 0 % - 0 �C
11    Name: CPU Core #8 - 0 % - 0 �C
12    Name: CPU Core #9 - 0 % - 0 �C
13    Name: CPU Core #10 - 0 % - 0 �C
14    Name: CPU Core #12 - 0 % - 0 �C
15    Name: CPU Total - 4,17 % - 0 �C
16    Name: CPU Package - 0 % - 0 �C
17Done.

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.


Comments

Twitter Facebook LinkedIn WhatsApp