Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically determining Mono runtime version

Tags:

c#

.net

mono

What is the recommended way to determine the Mono runtime version programmatically?

We have encountered various problems when our .Net application is used with older versions of Mono. Sometimes we can work around these problems, if we know which version we are dealing with, but sometimes we can't.

The solution for us would be to detect the Mono version programmatically, and then we can transparently apply the workarounds. If the Mono version is too old, then we would prompt the user to upgrade.

We can discover that Mono is our runtime with something like this:

bool isMonoRuntime = Type.GetType("Mono.Runtime") != null;

How can we also determine reliably the mono version without inferring it indirectly? To be clear, we need the Mono release number, not the .Net CLR version number.

like image 517
Stewart Avatar asked Dec 07 '11 10:12

Stewart


2 Answers

You can check the mono runtime version as:

Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
    MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
    if (displayName != null)
        Console.WriteLine(displayName.Invoke(null, null));
}

Check this How to determine the revision from which current Mono runtime was built and installed? for more details.

like image 152
Niranjan Singh Avatar answered Nov 07 '22 18:11

Niranjan Singh


Here is another solution (without using reflection)

[DllImport("__Internal", EntryPoint="mono_get_runtime_build_info")]
public extern static string GetMonoVersion();


Console.WriteLine("Mono version: {0}",GetMonoVersion());
Mono version: 3.12.0 (tarball Sat Feb  7 19:13:43 UTC 2015)
like image 33
Sergey Zhukov Avatar answered Nov 07 '22 20:11

Sergey Zhukov