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 anyPlatformID
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..
Next, create a new build configuration. You can copy the configuration from an existing one:
Then, right click on your Project and go to Properties. Under the build tab, define a 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:
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
The Calling Thread Must Be Sta, Because Many UI Components Require This in Wpf
How to Test If a Type Is Anonymous
How to Delete a Registry Value in C#
System.Data.Sqlclient.Sqlexception: Login Failed for User
Delegate Caching Behavior Changes in Roslyn
Why Is Only the UI Thread Allowed to Modify the Ui
Can You Specify Format for Xmlserialization of a Datetime
Datagrid Get Selected Rows' Column Values
Is SQLcommand.Dispose() Required If Associated SQLconnection Will Be Disposed
How to Interrupt Console.Readline