DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Send Email Using Spring Boot (SMTP Integration)
  • How To Scan and Validate Image Uploads in Java
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL

Trending

  • Send Email Using Spring Boot (SMTP Integration)
  • How To Scan and Validate Image Uploads in Java
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. NET Core – Cross-Platform Code

NET Core – Cross-Platform Code

.NET Core is cross platform, and in this short post, we will see a simple method to detect the underlying operating system on run-time.

Jawad Hasan Shani user avatar by
Jawad Hasan Shani
CORE ·
Mar. 24, 20 · Tutorial
Like (3)
Save
Tweet
Share
14.42K Views

Join the DZone community and get the full member experience.

Join For Free

.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:

  • Identify Operating System from .NET Core application
  • Execute related commands (.NET or Linux) based on OS and get results

Identify Operating System

.NET Core Console Application


Output of application on Windows OS


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.

Application Running on Linux Container

Getting Hardware Information

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.

Modified code to execute platform-specific functionality


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).

on Windows


on Docker Linux Image


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)

Windows Specific Code to get CPU information


Linux bash command


Extension Method to encapsulate bash execution functionality


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!

.NET code style Cross platform operating system application Linux (operating system) Net (command)

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Send Email Using Spring Boot (SMTP Integration)
  • How To Scan and Validate Image Uploads in Java
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: