How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement

64,563

Solution 1

[Editor's Note: This answer was applicable before .NET 4.7.1, or before the Windows Compatibility Pack for .NET Core was released. The current best answer is Alex Sanséau's to Stack Overflow question How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement.]

You can detect the execution platform using System.Environment.OSVersion.Platform:

public static bool IsLinux
{
    get
    {
        int p = (int) Environment.OSVersion.Platform;
        return (p == 4) || (p == 6) || (p == 128);
    }
}

From the Mono FAQ:

How to detect the execution platform

The execution platform can be detected by using the System.Environment.OSVersion.Platform value. However correctly detecting Unix platforms, in every cases, requires a little more work. The first versions of the framework (1.0 and 1.1) didn't include any PlatformID value for Unix, so Mono used the value 128. The newer framework 2.0 added Unix to the PlatformID enum but, sadly, with a different value: 4 and newer versions of .NET distinguished between Unix and macOS, introducing yet another value 6 for macOS.

This means that in order to detect properly code running on Unix platforms you must check the three values (4, 6 and 128). This ensure that the detection code will work as expected when executed on Mono CLR 1.x runtime and with both Mono and Microsoft CLR 2.x runtimes.

Solution 2

I found this recommendation on one of Microsoft's blogs:

We recommend you to use RuntimeInformation.IsOSPlatform() for platform checks.

Reference: Announcing the Windows Compatibility Pack for .NET Core

IsOSPlatform() takes an argument of types OSPlatform which has three values by default: Windows, Linux and OSX. It can be used as follow:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
  // Do something
}

The API is part of .NET Standard 2.0, and therefore available in .NET Core 2.0 and .NET Framework 4.7.1.

Solution 3

Use:

System.Environment.OSVersion

Solution 4

There isn't any such method, but you can use this method that checks it conditionally:

    public static OSPlatform GetOperatingSystem()
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            return OSPlatform.OSX;
        }

        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            return OSPlatform.Linux;
        }

        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            return OSPlatform.Windows;
        }

        throw new Exception("Cannot determine operating system!");
    }

Solution 5

You can use System.Environment.OSVersion to check what kind of platform you're on at runtime.

Share:
64,563
Bobbo
Author by

Bobbo

Updated on July 08, 2022

Comments

  • Bobbo
    Bobbo almost 2 years

    How do I determine what platform my C# code is running on? for example whether it is running on Linux or windows so that I can execute different code at runtime.

    I have a C# Windows application that I want to build to target Windows and Linux platforms.

    So far I have created two project files pointing to the same set of source code files. I then use a conditional compilation statement one of the projects called LINUX.

    Where there are difference in the actual code I use conditional statements using the conditional compilation statement, for example,

    #if (LINUX)
        ' Do something
    #endif
    

    Is there a better way of doing this? I don't really want to have two project files.

  • omajid
    omajid over 6 years
    So we probably shouldnt use 6 for IsLinux, then ;)
  • patrikbeno
    patrikbeno over 5 years
    That was 2011, today the correct answer is RuntimeInformation.IsOSPlatform(), as suggested by @Alex
  • Stefan Steiger
    Stefan Steiger about 5 years
    No, you can't, because it returns true for Unix/Linux if you're on Mac, and true for Mac if you're on Unix/Linux...
  • Cameron Taggart
    Cameron Taggart almost 5 years
    nuget.org/packages/… for many more platforms.
  • Eduardo Pignatelli
    Eduardo Pignatelli over 4 years
    Don't forget using System.Runtime.InteropServices;
  • Peter Mortensen
    Peter Mortensen over 4 years
    There is also a fourth option now, FreeBSD.
  • Naveen Kumar V
    Naveen Kumar V about 3 years
    Worked for .NET 5. :)
  • Joscha Götzer
    Joscha Götzer almost 3 years
    Shorter: public static bool IsLinux => (int) Environment.OSVersion.Platform is 4 or 6 or 128;
  • akash
    akash over 2 years
    Please provide explanation about your code that how can this solve the problem.
  • Ahmed Naeem
    Ahmed Naeem over 2 years
    It just simply return you a string, the name of the operating system. I don't think so any explanation is needed so far.
  • Ahmed Naeem
    Ahmed Naeem over 2 years
    the Vesrion() class represents the assembly version of your CLR