Determine Operating System in .Net Core

Determine runtime target (OS) in .NET Core app

Since I found no other way, I am using the value found in the .deps.json file. Here is my code:

using Newtonsoft.Json.Linq;
using System;
using System.IO;

/// <summary>
/// Returns the current RID (Runtime IDentifier) where this applications runs.
/// See for possible values, e.g. "ubuntu.18.04-x64".
/// The value is read from the first found .deps.json file in the application folder, at the path
/// "runtimeTarget"/"name" the value behind the last "/".
/// When the file or the value behind the last "/" is missing, this application folder was not compiled
/// for a specific runtime, and null is returned.
/// </summary>
public static string? GetRuntimeIdentifier() {
try {
// Find first (and probably only) .deps.json file in the application's folder.
var dir = AppDomain.CurrentDomain.BaseDirectory;
var files = Directory.GetFiles(dir, "*.deps.json");
if (files.Length == 0)
return null;
// Read JSON content
var json = JObject.Parse(File.ReadAllText(Path.Combine(dir, files[0])));
var name = json["runtimeTarget"]["name"].ToString();
// Read RID after slash
var slashPos = name.LastIndexOf('/');
if (slashPos == -1)
return null;
return name.Substring(slashPos + 1);
catch {
// Unexpected file format or other problem
return null;

How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement

[Editor's Note: This answer was applicable before .NET 4.7.1, or before the Windows Compatibility Pack for .NET Core was released. The current best answer is Alex Sanséau's to Stack Overflow question How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement.]

You can detect the execution platform using System.Environment.OSVersion.Platform:

public static bool IsLinux
int p = (int) Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);

From the Mono FAQ:

How to detect the execution platform

The execution platform can be detected by using the System.Environment.OSVersion.Platform value. However correctly detecting Unix platforms, in every cases, requires a little more work. The first versions of the framework (1.0 and 1.1) didn't include any PlatformID value for Unix, so Mono used the value 128. The newer framework 2.0 added Unix to the PlatformID enum but, sadly, with a different value: 4 and newer versions of .NET distinguished between Unix and macOS, introducing yet another value 6 for macOS.

This means that in order to detect properly code running on Unix platforms you must check the three values (4, 6 and 128). This ensure that the detection code will work as expected when executed on Mono CLR 1.x runtime and with both Mono and Microsoft CLR 2.x runtimes.

.NET Standard 2.0 get current Operating System

After further research and a better understanding of the Environment.OSVersion property, I now know that this question is, in fact, a duplicate of this one.

What led me to believing it was not is that, since the documentation doesn't show that these properties and methods are available in .NET Standard 2.0 i assume they were not and didn't take the time to try it.

So I simply copy/pasted the code from the second answer and saw that no error was thrown ...

How to detect OS my Asp.Net Core app is running on?

Rather than this, you should use Path.Combine to generate directory names. For example:

var root = env.WebRootPath;
var myDirectory = Path.Combine(root, "subdirectory");

How to determine if .NET Core is installed

Great question, and the answer is not a simple one. There is no "show me all .net core versions" command, but there's hope.


I'm not sure when it was added, but the info command now includes this information in its output. It will print out the installed runtimes and SDKs, as well as some other info:

dotnet --info

If you only want to see the SDKs: dotnet --list-sdks

If you only want to see installed runtimes: dotnet --list-runtimes

I'm on Windows, but I'd guess that would work on Mac or Linux as well with a current version.

Also, you can reference the .NET Core Download Archive to help you decipher the SDK versions.

Everything below this point is old information, which is less relevant, but may still be useful.

See installed Runtimes:

Open C:\Program Files\dotnet\shared\Microsoft.NETCore.App in Windows Explorer

See installed SDKs:

Open C:\Program Files\dotnet\sdk in Windows Explorer

(Source for the locations: A developer's blog)

In addition, you can see the latest Runtime and SDK versions installed by issuing these commands at the command prompt:

dotnet Latest Runtime version is the first thing listed. DISCLAIMER: This no longer works, but may work for older versions.

dotnet --version Latest SDK version DISCLAIMER: Apparently the result of this may be affected by any global.json config files.

On macOS you could check .net core version by using below command.

ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/

On Ubuntu or Alpine:

ls /usr/share/dotnet/shared/Microsoft.NETCore.App/

It will list down the folder with installed version name.

C# - Get Operating System from IP address / DNS Name

What you are doing is similar to a CMDB inventory scan, and there are different strategies for windows and linux. For starters, decide on whether to go agent or agent-less.

If you go the agent route, then write a small .net application that exposes a port to query and provide an OS specific implementation for both. Then get you IT department to install it on everything.

Agent-less is a bit more tricky. Start by looking for ports that should be open for management functions (WinRM on Windows - though be careful here - it's turned off by default, SSH on Linux), or any services you know your IT department provision by default (hopefully on their base VM images). If WinRM/SSH is enabled, and you have password store you can programatically query, then you can perform remote logins and run any code you need to extract any further information. As you say you don't have the passwords, the best you can do is guess based on exposed ports.

Related Topics

Leave a reply