Determine Operating System in .NET Core

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

Leave a Comment