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

c# get os version windows 10
.net core detect linux or windows
net core conditional compilation
.net check operating system
c# is windows os
.net windows version
get os name using c#
csproj condition linux

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.

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

How to check the OS version at runtime eg windows or linux without , How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement. Question. Does anyone know how to determine� I have a c# windows app that I want to build to target windows and linux platforms. So far I have created 2 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 coditional statements using the conditional

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.

Platform Conditional Compilation in .NET Core, NET Core 1.0 introduced the System.Runtime.InteropServices. values like this: If you want to perform this check at compile time in… for a specific OS, you can do this using conditional compilation directives: NET Core SDK on either Windows or Linux depending on what Thanks for this example! How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement Tags ajax android angular api button c++ class database date dynamic exception file function html http image input java javascript jquery json laravel list mysql object oop ph php phplaravel phpmysql phpphp post python sed select

Use:

System.Environment.OSVersion

Conditional Compilation, Any DeclarationBlock or Statement that is not compiled in still must be to group a set of features under one major version, for example: Windows, Microsoft Windows systems Posix, All POSIX systems (includes Linux, FreeBSD, OS X, Solaris, etc.) The debug build includes extra error checking code, test harnesses,� 49 How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement Nov 20 '17 21 Set index.html as the default page Jan 19 '15 5 Office.context.document.getFileAsync throwing errors Feb 26 '15

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

Platform dependent compilation - Unity, Editor code on Windows. UNITY_EDITOR_OSX, #define directive for Editor code on Mac OS X. Here is an example of #define directives exposed in Unity 5.0.1: Build Settings window with PC, Mac & Linux selected as the target platforms NET 4.x Eqivalent scripting runtime version compiler, csc is used with csc.rsp . How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement ; Working with multiple versions of Visual Studio ; Put WPF control into a Windows Forms Form

To expand on other answers, in cases where a Linux and Windows implementation of a feature are not compatible (that is, require references to libraries only available for a specific platform), you can also use an interface and have two separate assemblies, one written and compiled on each platform, with a type that implements this interface.

Then, based on the check, use Assembly.Load() to load only the right assembly (and its platform-specific dependency), reflection to find your type in the assembly, and Activator.CreateInstance() to get an instance of the type that you can then work with normally.

How to use conditional compilation with the go build tool, This post explains how conditional compilation is implemented and show you as go build , test , and install but does not perform any compilation. of files in os /exec package that would be compiled on this linux/arm system. If I were to run the same command on a Windows system, the result would be Version number¶. The version numbers of the Linux and Windows versions of Zero Install are loosely coupled. If the first two components of the version number are the same (e.g. 2.1 and 2.1.3) they generally support the same features.

Conditional compilation, Key-value option set at most once with the target's operating system value. Example values: "unix"; "windows". unix and windows. unix� You can have a Windows 10 configuration with and without Hyper-V as follows in an Admin prompt: bcdedit /copy {current} /d "Windows 10 no Hyper-V" find the new id of the just created “Windows 10 no Hyper-V” bootentry, eg. {094a0b01-3350-11e7-99e1-bc5ec82bc470} bcdedit /set {094a0b01-3350-11e7-99e1-bc5ec82bc470} hypervisorlaunchtype Off

Cross-Platform Development Tips, Please see the "setup.h" file and "install.txt" files for details on configuration. On Linux, Mac and OS/2, you use the 'configure' command to generate the necessary On Windows using a compiler other than MinGW/Cygwin, you would build the wxWidgets does not use C++ run-time type information since wxWidgets� I found this link How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement. System.Environment.OSVersion.Platform can be used for this purpose. share | improve this answer

Cross-platform software, In computing, cross-platform software is computer software that is implemented on multiple An example of a common platform is Microsoft Windows running on the x86 Software platforms can either be an operating system or programming in such a way that one test case is usable for multiple versions of an app. Going through the using statement takes a significant amount of time Performing the first inference takes a significant amount of time (maybe ~25X slower than not using TensorRT) Then each subsequent pass through the network has a 25% reduction in latency as compared to not using TensorRT

Comments
  • So we probably shouldnt use 6 for IsLinux, then ;)
  • That was 2011, today the correct answer is RuntimeInformation.IsOSPlatform(), as suggested by @Alex
  • nuget.org/packages/… for many more platforms.
  • Don't forget using System.Runtime.InteropServices;
  • There is also a fourth option now, FreeBSD.
  • 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...