Edit Page
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 |
+ | 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 |
+ | 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 |
+ | 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 |
+ | 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]] |