Determine Operating System in .NET Core Ask Question

Determine Operating System in .NET Core Ask Question

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?

ベストアンサー1

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;

例えばosNameAndVersionMicrosoft Windows 10.0.10586

おすすめ記事