.NET Core is cross platform and in this short post I will describe a simple method to detect the underlying operating system on run-time.
There are situations, when we want to run a different piece of code based on the Operating System. This capability could be useful in various scenarios e.g. I wanted to obtain the underlying Hardware Information in order to bind it to a licensing mechanism. While its true .NET Core already support a wide variety of operations, but for this particular use case, there is yet no support to obtain this information cross platform.
So, the route I took is simple in nature and doesn’t require complex programming and gets the job done. So, I will describe the idea and feel free to refactor it as per your use case and appetite of code cleanness.
So, I knew, that on windows side of things System.Management Namespace provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure. You can check more details on https://docs.microsoft.com/en-us/dotnet/api/system.management?view=netframework-4.8
However System.Management namespace will not work if would you would like to use it in Linux. There is already a github thread about bringing this functionality in .NET core and you can follow it on https://github.com/dotnet/corefx/issues/22660
So, how we can achieve this functionality today and without complexity? The method I used is to use existing System.Management namespace and functionality it provides to obtain hardware information on windows side of things. For Linux, we can use bash command to get the same results. So, we want to execute bash commands from .NET Core application and alone this capability is very powerful.
Lets break down this in high-level tasks:
- Identify Operating System from .NET Core application
- Execute related commands (.NET or Linux) based on OS and get results
Identify Operating System
For testing this piece of code on Linux, I used Docker Linux – Ubuntu and Linux-armv7 debian image due to simple usage, but result should be same, if you manually deploy the published application to these OS.
Find below the screenshots of running the same application on Linux . To keep things simple, Docker and how to publish .NET Core application discussion is outside the scope of this post.
Getting Hardware Information
Regardless of title of this section, this part will also demo that how to execute bash command on Linux via .NET Core. Here is slightly modified code from previous example. Note I left out the part for MAC as the concept is same and you can implement it following same pattern.
The modified program, in general is again simple, we wrap the platform specific functionality in separate classes and then simply used those in corresponding block. Here is the output when executed both on Windows and Linux. (Note I am reading slightly different data from both platform, again it can be anything).
The composition itself is very simple, you execute whatever logic you want to based on the platform. Check below the corresponding code CPUInformationChecker (windows and Linux parts)
So, this was a very simple way to execute platform specific code in certain situations. The above mentioned technique can be optimized more, you can also use conditional compilation flags if you want to.
Feel free to ask in comment, if something is not clear. Happy Coding!
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.