[Mono-list] Determining the platform at compile and run time

A Rafael D Teixeira rafaelteixeirabr@hotmail.com
Thu, 30 Oct 2003 22:34:28 -0200


Here is a small twist, save it as Mono.Environment.cs and compile as a 
library to use or as an executable to just test:

-------
// How can I tell dynamically what platform my code is running on?
//
// This is one possible approach, contributed by Aleksey Demakov 
(avd@openlinksw.com)
// And encapsulated by Rafael Teixeira (rafaelteixeirabr@hotmail.com)

using System;

namespace Mono {

	public enum RunningPlatform { MonoOnUnix, MonoOnWin32, DotNet };

	public class Environment {

		public static RunningPlatform RunningPlatform {
			get {
				Type enumType = typeof(PlatformID);

				if (Enum.IsDefined (enumType, "Unix")) {
				    if (System.Environment.OSVersion.Platform == (PlatformID) 
Enum.Parse(enumType, "Unix"))
						return RunningPlatform.MonoOnUnix;
					else
						return RunningPlatform.MonoOnWin32;
				}
				return RunningPlatform.DotNet;
			}
	 	}

		public static void Main(String[] parameters) {
			Console.WriteLine("Running in " + 
Environment.RunningPlatform.ToString());
		}

	}
}

-------

on my RedHat9, obviously as a program it prints:

-------
[rafael@redhat9 desktop]$ mcs Mono.Environment.cs
Compilation succeeded
[rafael@redhat9 desktop]$ mono Mono.Environment.exe
Running in MonoOnUnix
-------

best regards,

Rafael Teixeira
Brazilian Polymath
Mono Hacker since 16 Jul 2001
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/

_________________________________________________________________
MSN Messenger: instale grátis e converse com seus amigos. 
http://messenger.msn.com.br