Mono Wiki
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
The execution platform can be detected by using the <code>System.Environment.OSVersion.Platform</code> 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 <code>PlatformID</code> value for Unix, so Mono used the value 128. The newer framework 2.0 added Unix to the <code>PlatformID</code> 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.
+
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 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.
+
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.
   
   
   
Note <code>System.Environment.OSVersion.Platform</code> may return <code>PlatformID.Unix</code> for Mac OSX systems.
+
Note System.Environment.OSVersion.Platform may return PlatformID.Unix for Mac OSX systems.
 
   
  +
   
 
<source lang=csharp>using System;
 
<source lang=csharp>using System;
Line 16: Line 16:
 
{
 
{
 
int p = (int) Environment.OSVersion.Platform;
 
int p = (int) Environment.OSVersion.Platform;
if ((p == 4) || (p == 6) || (p == 128)) {
+
if ((p == 4) || (p == 128)) {
 
Console.WriteLine ("Running on Unix");
 
Console.WriteLine ("Running on Unix");
 
} else {
 
} else {
Line 24: Line 24:
 
}</source>
 
}</source>
   
  +
Second example:
 
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 [http://mono-project.com/FAQ%3a_Technical#Mono_Platforms Mono FAQ] - ''How to detect the execution platform''.
 
 
 
Second example from MSDN (does not check for MacOS):
 
 
<source lang=csharp>
 
<source lang=csharp>
   
Line 66: Line 58:
 
}
 
}
 
</source>
 
</source>
[[category:Tutorials]]
 
 
[[category:Tutorials]]
 
[[category:Tutorials]]
Please note that all contributions to the Mono Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)
Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above.

View this template