.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 that .NET Core already supports a wide variety of operations, in this particular use case, there is 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. I will describe the idea and feel free to refactor it as per your use case and appetite of code cleanness.
I knew that on the 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 out 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
How can we achieve this functionality today and without complexity? The method I used is to use the existing System.Management namespace and functionality it provides to obtain hardware information on the windows side of things. For Linux, we can use the bash command to get the same results. So, we want to execute bash commands from the .NET Core application, and alone, this capability is very powerful.
Let's break down this in high-level tasks:
For testing this piece of code on Linux, I used Docker Linux – Ubuntu and Linux-armv7 debian image due to simple usage, but the result should be the 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, a Docker and how to publish .NET Core application discussion is outside the scope of this post.
Regardless of the title of this section, this part will also demo how to execute the bash command on Linux via .NET Core. Here is a slightly modified code from the previous example. Note I left out the part for MAC, as the concept is the same, and you can implement it following the same pattern.
The modified program, in general, is again simple; we wrap the platform-specific functionality in separate classes and then simply use those in the 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, and you can also use conditional compilation flags if you want to.
Feel free to ask in the comments if something is not clear. Happy coding!