We're Getting Mutants in the MCU - The Loop


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 X, introducing yet another value 6 for MacOS X.

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.

Note System.Environment.OSVersion.Platform may return PlatformID.Unix for Mac OSX systems.

using System;
class Program {
        static void Main ()
                int p = (int) Environment.OSVersion.Platform;
                if ((p == 4) || (p == 6) || (p == 128)) {
                        Console.WriteLine ("Running on Unix");
                } else {
                        Console.WriteLine ("NOT running on Unix");

Notice that as of Mono 2.2 the version returned on MacOS X is still 4 for legacy reasons, too much code was written between the time that the MacOSX value was introduced and the time that we wrote this text which has lead to a lot of user code in the wild to not cope with the newly introduced value.

A better way of testing for Unixness is to make tests that are feature specific instead of dividing the code in Unix vs Windows. For example, for file system operations, it is better to use the path character separator and compare it for '/' or '\' as that would not depend on the actual enumeration value.

See Mono FAQ - How to detect the execution platform.

Second example from MSDN (does not check for MacOS):

// This example demonstrates the PlatformID enumeration.
using System;

class Sample 
    public static void Main() 
    string msg1 = "This is a Windows operating system.";
    string msg2 = "This is a Unix operating system.";
    string msg3 = "ERROR: This platform identifier is invalid.";

    OperatingSystem os = Environment.OSVersion;
    PlatformID     pid = os.Platform;
    switch (pid) 
        case PlatformID.Win32NT:
        case PlatformID.Win32S:
        case PlatformID.Win32Windows:
        case PlatformID.WinCE:
        case PlatformID.Unix:
Community content is available under CC-BY-SA unless otherwise noted.