[Mono-devel-list] Determine the name and/or kind of CLI runtime environment

Kornél Pál kornelpal at hotmail.com
Wed Jun 1 06:16:03 EDT 2005


There seems not to be a good way to determine the name and version of the
runtime, but I think the above code is good enough.

> From: Ben Maurer
> No, not today (other than my --version hack, which actually isn't all
> that bad).

--version seems to be the only way to obtain the version of Mono runtime.
But using bash is not a good idea I think as it makes the code bash
dependent.

I tested this code on Windows and should work on Linux as well. Windows
executes the same mono.exe as Windows searches the directory of the
executable (mono.exe in this case) for the executable. I think this is not
the same on Linux. How could it be ensured to execute the same mono?

Kornél

string runtimeName = typeof(object).GetType().FullName;
Version runtimeVersion = Environment.Version;

switch (runtimeName)
{
 case "System.RuntimeType":
  runtimeName = "Microsoft .NET Framework";
  break;
 case "System.MonoType":
 {
  runtimeName = "Mono";
  try
  {
   ProcessStartInfo startInfo = new ProcessStartInfo("mono", "--version");
   startInfo.UseShellExecute = false;
   startInfo.RedirectStandardOutput = true;
   startInfo.CreateNoWindow = true;

   Process runtimeProcess = null;
   try
   {
    runtimeProcess = Process.Start(startInfo);
    if (runtimeProcess != null)
    {
     Match versionMatch =
Regex.Match(runtimeProcess.StandardOutput.ReadLine(),
@"\d{1,10}\.\d{1,10}\.\d{1,10}\.\d{1,10}", RegexOptions.CultureInvariant);
     if (versionMatch.Success)
      try
      {
       runtimeVersion = new Version(versionMatch.Value);
      }
      catch (OverflowException)
      {
      }
    }
   }
   catch (Win32Exception)
   {
   }
   finally
   {
    if (runtimeProcess != null)
     runtimeProcess.Close();
   }
  }
  catch (SecurityException)
  {
  }
  break;
 }
 case "System.Reflection.ClrType":
  runtimeName = "DotGNU Portable.NET";
  break;
 default:
  runtimeName = "<" + runtimeName + ">";
  break;
}
Console.WriteLine("Runtime: {0} {1}", runtimeName, runtimeVersion);




More information about the Mono-devel-list mailing list