191

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?

3

3 Answers 3

294

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

4
  • 6
    You can add that to get more information on OS there's another property in that package: System.Runtime.InteropServices.RuntimeInformation.OSDescription - returns description of OS with version, etc. Aug 5, 2016 at 13:54
  • 24
    +1 although I do not like this answer. Why cant they just implement System.Environment.OSVersion.Platform for consistency?
    – leppie
    Aug 5, 2016 at 18:28
  • 2
    Note that the constants do not represent all of the supported OSes. It is possible to probe for other OSes by using 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. Sep 24, 2017 at 4:49
  • 5
    Beware! 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 stackoverflow.com/a/66618677/56621 Sep 28, 2021 at 16:59
57

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
2
  • 6
    this should be the top answer Sep 28, 2021 at 16:58
  • 1
    Only if you're targetting .NET 5 or later. The API unfortunately doesn't exist in .NET Core or any of the .NET Standards
    – lethek
    May 13, 2022 at 3:50
50

System.Environment.OSVersion.Platform can be used in full .NET Framework and Mono but:

  • Mac OS X detection almost never worked for me under Mono
  • it is not implemented in .NET Core

System.Runtime.InteropServices.RuntimeInformation can be used in .NET Core but:

  • it is not implemented in full .NET Framework and Mono
  • it does not perform platform detection in runtime but uses hardcoded information instead
    (see corefx issue #3032 for more details)

You could pinvoke platform specific unmanaged functions such as uname() but:

  • it may cause segmentation fault on unknown platforms
  • is not allowed in some projects

So my suggested solution (see code bellow) may look sily at first but:

  • it uses 100% managed code
  • it works in .NET, Mono and .NET Core
  • it works like a charm so far in Pkcs11Interop library
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();
}
4
  • 1
    Appreciate for your effort. Wish there can be some consistency in the future.
    – leppie
    Aug 5, 2016 at 18:31
  • 8
    System.Runtime.InteropServices.RuntimeInformation should work correctly in full .net now (since November), so that seems to be the accepted "right" way now. Not sure about mono, but since they are taking some code direct from .net now that it is open source, it's only a matter of time before it's working there, if not already. Mar 9, 2017 at 1:54
  • 3
    Path.DirectorySeparatorChar Can be used to determine if its a windows or *nix machine.
    – kiran
    Nov 20, 2018 at 14:57
  • InteropServices are very strange. Within VStudio and Rider, I sometimes get "Unknown in this context" or it compiles. No idea about the reason of this failure...
    – Slesa
    Mar 5, 2020 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.