Determine Operating System in .NET Core

dknaack picture dknaack · Aug 5, 2016 · Viewed 50.4k times · Source

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?

Answer

dknaack picture dknaack · Aug 5, 2016

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