[Mono-devel-list] Environment.OSVersion.Platform for Unix
Sebastien Pouliot
spouliot at videotron.ca
Mon May 9 11:12:54 EDT 2005
Hello,
The original frameworks (both 1.0 and 1.1) didn't have a value to
identity Unix (and Unix-like) operating systems. Mono has been using the
value 128 up to now to identity them.
The framework v2.0 introduce a new Unix enum value (4) in PlatformID,
but this isn't compatible with the existing Mono value [1]. As you can
guess this is a "breaking change" for Mono.
So the default Mono profile, corresponding to Fx 1.1, will still
continue to use the 128 value (for compatibility with existing code).
However the newer 2.0 profile has started (it's in SVN but it wasn't in
1.1.7) to use the new Unix (4) value.
There are a few ways for applications to deal with this:
1. You can use a compiler define to deal with 1.1/2.0 differences. Some
code already have to deal with this (so it's no big deal), e.g. this is
what you'll see in the class libraries (except for corlib) and the unit
tests.
using System;
class Program {
static void Main ()
{
#if NET_2_0
if (Environment.OSVersion.Platform == PlatformID.Unix) {
#else
if ((int) Environment.OSVersion.Platform == 128) {
#endif
Console.WriteLine ("Running on Unix");
} else {
Console.WriteLine ("NOT running on Unix");
}
}
}
% mcs test1.cs /d:NET_1_1
% mono test1.exe
Running on Unix
% gmcs test1.cs /d:NET_2_0
% mono test1.exe
Running on Unix
Note: inside mscorlib.dll we can use the internal
Environment.IsRunningOnWindows to shield us from the difference.
2. Lots of existing code don't care about the framework version. In that
case it's easier to use code that will work on both versions, like:
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");
}
}
}
% mcs test2.cs
% mono test2.exe
Running on Unix
% gmcs test2.cs
% mono test2.exe
Running on Unix
3. If your code can only be compiled with 2.0 (e.g. it use generics)
then you can use the new enum value Unix (4) and forget about the old
value.
using System;
class Program {
static void Main ()
{
// this will only work with 2.0 (for both Mono and MS)
if (Environment.OSVersion.Platform == PlatformID.Unix) {
Console.WriteLine ("Running on Unix");
} else {
Console.WriteLine ("NOT running on Unix");
}
}
}
% gmcs test3.cs
% mono test3.exe
Running on Unix
... but this won't work if you try to compile it with MCS ...
mcs test3.cs
test3.cs(15) error CS0117: `System.PlatformID' does not contain a
definition for `Unix'
Compilation failed: 1 error(s), 0 warnings
... or CSC versions earlier than 8.0.*
[1] http://bugzilla.ximian.com/show_bug.cgi?id=74841
--
Sebastien Pouliot <sebastien at ximian.com>
blog: http://pages.infinit.net/ctech/poupou.html
More information about the Mono-devel-list
mailing list