[Mono-devel-list] Environment.OSVersion.Platform for Unix

Michi Henning michi at zeroc.com
Tue May 10 00:52:56 EDT 2005


Miguel de Icaza wrote:
> Hello,
> 
> 
>>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
> 
> 
> This approach is strongly recommended, and its the one that we should be
> using inside our class libraries to avoid people mimicing the use of the
> NET_2_0 define which will not be there for them.

Another way to do this is to invert the test and avoid the issue entirely:

	using System;

	PlatformID id = Environment.OSVersion.Platform;
	if(   id == PlatformID.Win32NT
	   || id == PlatformID.Win32S
	   || id == PlatformID.Win32Windows
	   || id == PlatformID.WinCE)
	{
	    Console.WriteLine ("NOT running on Unix");
	}
	else
	{
	    Console.WriteLine ("Running on Unix");
	}

Cheers,

Michi.




More information about the Mono-devel-list mailing list