Determine Operating System in .NET Core

C#.Net Core

C# Problem Overview


How can I determine which operating system my .NET Core app is running on? In the past I could use Environment.OSVersion.

What is the current way to determine whether my app is running on Mac or Windows?

C# Solutions


Solution 1 - C#

Method

System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform()

Possible Argument

OSPlatform.Windows
OSPlatform.OSX
OSPlatform.Linux

Example

bool isWindows = System.Runtime.InteropServices.RuntimeInformation
                                               .IsOSPlatform(OSPlatform.Windows);

Update

Thanks to the comment by Oleksii Vynnychenko

You can get the operating systems name and version as a string using

var osNameAndVersion = System.Runtime.InteropServices.RuntimeInformation.OSDescription;

E.g. osNameAndVersion would be Microsoft Windows 10.0.10586

Solution 2 - C#

System.Environment.OSVersion.Platform can be used in full .NET Framework and Mono but:

  • Mac OS X detection almost never worked for me under Mono
  • it is not implemented in .NET Core

System.Runtime.InteropServices.RuntimeInformation can be used in .NET Core but:

  • it is not implemented in full .NET Framework and Mono
  • it does not perform platform detection in runtime but uses hardcoded information instead
    (see corefx issue #3032 for more details)

You could pinvoke platform specific unmanaged functions such as uname() but:

  • it may cause segmentation fault on unknown platforms
  • is not allowed in some projects

So my suggested solution (see code bellow) may look sily at first but:

  • it uses 100% managed code
  • it works in .NET, Mono and .NET Core
  • it works like a charm so far in Pkcs11Interop library

string windir = Environment.GetEnvironmentVariable("windir");
if (!string.IsNullOrEmpty(windir) && windir.Contains(@"\") && Directory.Exists(windir))
{
	_isWindows = true;
}
else if (File.Exists(@"/proc/sys/kernel/ostype"))
{
	string osType = File.ReadAllText(@"/proc/sys/kernel/ostype");
	if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
	{
		// Note: Android gets here too
		_isLinux = true;
	}
	else
	{
		throw new UnsupportedPlatformException(osType);
	}
}
else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
{
	// Note: iOS gets here too
	_isMacOsX = true;
}
else
{
	throw new UnsupportedPlatformException();
}

Solution 3 - C#

Check System.OperatingSystem class it has static methods for each OS i.e. IsMacOS(), IsWindows(), IsIOS() and so on.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondknaackView Question on Stackoverflow
Solution 1 - C#dknaackView Answer on Stackoverflow
Solution 2 - C#jariqView Answer on Stackoverflow
Solution 3 - C#Sameer VartakView Answer on Stackoverflow