PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How do I get Hardware Info with .NET 5 in a Cross Platform Way?

 September 12, 2022     .net, c#, cross-platform, linux, windows     No comments   

Issue

I'm developing a .NET 5 console application and would like to be able to read some basic system info such as CPU temperature, memory usage, CPU usage, etc. My development machine is running windows but the application is deployed to a Linux machine, therefore I'd like to avoid having to write two different sets of code for both OS.

How do I do this in a cross platform way? There are plenty of Windows API and calls that will do this, but I need Linux to work as well.


Solution

There is a github issue open to standardize this as part of the .net runtime. Maybe we'll see this in .net 6 or 7, who knows... https://github.com/dotnet/runtime/issues/22948

This is one of the better libraries: https://github.com/openhardwaremonitor/openhardwaremonitor. They appear to have attempted some Linux support.

For things like CPU usage you may have success with How to get the CPU Usage in C#? (Windows).

For CPU on Linux, parsing result of command line: https://phoenixnap.com/kb/check-cpu-usage-load-linux. In short:

sudo apt install sysstat
mpstat 1

The GC class has methods to get memory used by C# runtime. For system memory, here are a couple resources:
Windows: How do you get total amount of RAM the computer has?
Linux: Run free -m and parse result using Process class.

Other links that might be helpful for Linux:

  • https://raspberrypi.stackexchange.com/questions/117094/raspberry-pi-with-linux-and-c-on-it-how-to-read-hardware-sensor-info
  • How to identify the hardware details of a Linux/Mac machine using .Net Core


Answered By - jjxtra
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing