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?
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?
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform()
OSPlatform.Windows
OSPlatform.OSX
OSPlatform.Linux
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(OSPlatform.Windows);
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
System.Runtime.InteropServices.RuntimeInformation.OSDescription
- returns description of OS with version, etc. –
Christan System.Environment.OSVersion.Platform
for consistency? –
Psycholinguistics IsOSPlatform(OSPlatform.Create("FreeBSD"))
whether they are supported now or may be added in the future. However, it is not very clear what a safe approach would be for what strings to pass (for example, does case matter, or does "bsd"
match both "FreeBSD"
and "NetBSD"
?). See discussion about this feature here. –
Bio RuntimeInformation.IsOSPlatform
does not look at the current OS but rather checks the build configuration target. Proof: github.com/dotnet/runtime/blob/… Check this answer https://mcmap.net/q/134332/-determine-operating-system-in-net-core –
Hester Check System.OperatingSystem
class it has static methods for each OS i.e. IsMacOS()
, IsWindows()
, IsIOS()
and so on. These methods are available starting with .NET 5.
This makes it a great choice because the implementations for these methods use preprocessor directives to fix the return value to a constant true/false at compilation time for each target OS the OperatingSystem
class is compiled for. There is no runtime probing or calls to make.
Here is an excerpt from one such method in OperatingSystem
:
/// <summary>
/// Indicates whether the current application is running on Linux.
/// </summary>
[NonVersionable]
public static bool IsLinux() =>
#if TARGET_LINUX && !TARGET_ANDROID
true;
#else
false;
#endif
System.Environment.OSVersion.Platform
can be used in full .NET Framework and Mono but:
System.Runtime.InteropServices.RuntimeInformation
can be used in .NET Core but:
You could pinvoke platform specific unmanaged functions such as uname()
but:
So my suggested solution (see code bellow) may look sily at first but:
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();
}
© 2022 - 2024 — McMap. All rights reserved.
Environment.OSVersion
works in .NET Core 2.0+. – Roid