Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Operating System in .NET Core

Tags:

c#

.net-core

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?

like image 466
dknaack Avatar asked Oct 19 '22 08:10

dknaack


People also ask

Which operating systems is .NET Core compatible with?

NET on Android, Apple, Linux, and Windows operating systems. It can be used on Arm64, x64, and x86 architectures. It's also supported in emulated environments, like macOS Rosetta 2. New versions of .

How do I know if an application is .NET Core?

Here by dependencies one can determine about the type of project.. NET Core application do reference Microsoft. NETCore. App (either as "type": "platform" for portable apps or without it for self-contained apps).

What is operating system in C#?

SharpOS is a discontinued computer operating system based on the . NET Framework and related programming language C#.


1 Answers

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

like image 261
dknaack Avatar answered Oct 21 '22 21:10

dknaack