How to Check the Os Version at Runtime, E.G. on Windows or Linux, Without Using a Conditional Compilation Statement

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

[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.

Conditional compilation in C++ based on operating system

My gcc (4.3.3) defines the following Linux-related predefined macros:

$ gcc -dM -E - < /dev/null | grep -i linux
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1

Under VC++ (and many other Win32 compilers) there are also a couple of predefined macros identifying the platform, most notably _WIN32. Further details: http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx

Visual studio conditional compilation for os

This answer assumes you're asking about custom pre-processor symbols (that is how I have interpreted it - do correct me if I am wrong.

You could use a custom build configuration:

Start by going into the build Configuration Manager..

Configuration Manager

Next, create a new build configuration. You can copy the configuration from an existing one:

Create new configuration

Then, right click on your Project and go to Properties. Under the build tab, define a conditional compilation symbol:

Conditional compilation symbol

Do the same for Windows.

Then you can write conditional steps like the below:

    class Program
{
static void Main(string[] args)
{
#if MACOS
Console.WriteLine("OSX");
#elif WINDOWS
Console.WriteLine("Windows");
#endif

Console.Read();
}

Depending on your chosen build configuration .. you'll either get:

OSX:

OSX

Windows:

Windows

How to add conditional compilation symbols regarding OS platforms in .Net Standard projects

A .NET Standard library doesn't know at compile time that it is going to be used by Android, iOS, or anything else. That's why you can't use conditional compilation symbols.

If you are using Xamarin Forms you can use the Device class to make runtime decisions based on the platform that is actually running.



Related Topics



Leave a reply



Submit