[Mono-list] mono or .Net

Jonathan Pryor jonpryor@vt.edu
Sun, 05 Dec 2004 11:57:45 -0500


On Sat, 2004-12-04 at 17:42 +0100, Ivan Turcan wrote: 
> How can i determine within my application if is running uder Mono or
> under Net ? or if i am running on Windows/Linux/Mac ?? exists here
> some namespace/methods or so ?

The first question is this: do you really need to determine that?  It is
preferable to write platform-agnostic code whenever possible.

Of course, reality intrudes, and you frequently do need to know the
underlying platform.  There are two primary ways I've seen this done:

1.  Check System.IO.Path.DirectorySeparatorChar.  This is backslash (\)
on Windows, and forward slash (/) on Unix.

2.  Check System.Environment.OSVersion.Platform.  This is 128 when
running under Mono on Unix platforms, but this value may not be portable
across the various Unix .NET efforts (Rotor, Portable.NET, Mono).

Feature checks should still be preferred to platform checks.  You could
always just DllImport any native libraries you need, and if the method
isn't present a System.MissingMiethodException exception will be thrown.
You can check for this and fallback to alternate implementations, if
desirable.

 - Jon