Detecting the execution platform
From Mono Wiki
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.
This means that in order to detect properly code running on Unix platforms you must check both values (4 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.
using System; class Program { static void Main () { int p = (int) Environment.OSVersion.Platform; if ((p == 4) || (p == 128)) { Console.WriteLine ("Running on Unix"); } else { Console.WriteLine ("NOT running on Unix"); } } }
Second example:
// 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: Console.WriteLine(msg1); break; case PlatformID.Unix: Console.WriteLine(msg2); break; default: Console.WriteLine(msg3); break; } } }
