How to Detect Which .Net Runtime Is Being Used (Ms VS. Mono)

How to detect which .NET runtime is being used (MS vs. Mono)?

From the Mono Project's Guide to Porting Winforms Applications:

public static bool IsRunningOnMono ()
{
return Type.GetType ("Mono.Runtime") != null;
}

I'm sure you'll have a lot more questions, so worth checking this guide and the mono-forums

How to figure out if running in Mono runtime

You can use reflection to check if the Mono.Runtime type is defined. Like this:

Type.GetType("Mono.Runtime")

The mono project FAQ recommends this method to detect if you are running in mono

Good luck!

How do I check .net/mono version from within .net

In Windows the most reliable way to determine the installed .NET framework version is to use the registry. The subkey you need to check is:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\

Check out this link for a sample of code to read these values correctly in C#:

http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx

In Mono the most reliable way to determine the version is probably by running the shell command:

mono --version

I guess you could run that with the Process class and parse the output.

How do I know which version of .NET mono is using?

Mono is an implementation of .NET.

Lets re-phrase that: Mono is a .NET implementation. It doesn't use an external .NET implementation, it is a .NET implementation.

Mono wont use .NET Core, or .NET 5. Those are separate implementations and runtimes for .NET. If you want to use .NET 5, install and use .NET 5.

Check the Mono releases page for information about Mono release versions and which .NET Framework release they match up to.

For example, Mono 5.18.0 added suppport for .NET Framework 4.7.2 and Mono 6.6.0 added support for .NET 4.8. But please be aware that Mono won't be 100% compatible.

It sounds like this might be the first time you are using Mono. If so, watch out. Mono is far from 100% compatible with .NET Framework. There are missing features, bugs, performance issues and so on. It works for many use-cases, but it's not really supported by Microsoft.

How to find out which version of the .NET Framework an executable needs to run?

I think the closest you can reliably get is to determine what version of the CLR is required. You can do this by using ILDASM and looking at the "MANIFEST" node or Reflector and looking at the dissasembly view of the "Application.exe" node as IL. In both cases there is a comment that indicates the CLR version. In ILDASM, the comment is "// Metadata version" and in Reflector the comment is "Target Runtime Version".

Here are examples for a .NET WinForms application named WindowsFormsApplication1.exe:

ILDASM:

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 2:0:0:0
}
.assembly extern System
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 2:0:0:0
}

Reflector:

.module WindowsFormsApplication1.exe
.subsystem 0x0002
// MVID: {CA3D2090-16C5-4899-953E-4736D6BC0FA8}
// Target Runtime Version: v2.0.50727

You can also look at the list of referenced assemblies and look for the reference with the highest version number.

Again, using ILDASM looking at the "MANIFEST" node data:

.assembly extern System.Drawing
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) // .?_....:
.ver 2:0:0:0
}
.assembly extern System.Core
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 3:5:0:0
}

And using Reflector, looking at the dissambly (still as IL) for each reference listed:

.assembly extern System.Core
{
.ver 3:5:0:0
.publickeytoken = (B7 7A 5C 56 19 34 E0 89)
}

By finding the reference with the highest version metadata you can determine what version of the Framework that reference came from, which would indicate that you need the same version of the Framework installed for the application to run. That being said, I wouldn't treat this as 100% reliable, but I don't think it will change any time soon.

How to detect what platform my C#/F# code is running on mono or .NET?

You shouldn't have to do this. However, if it's really necessary you can check for the type Mono.Runtime. From the Mono FAQ:

Type t = Type.GetType ("Mono.Runtime");
if (t != null)
Console.WriteLine ("You are running with the Mono VM");
else
Console.WriteLine ("You are running something else");

How do I tell my code is running on Mono?

From the Mono FAQ:

http://www.mono-project.com/FAQ:_Technical

Below is directly from that link:

How can I detect if am running in Mono?

Having code that depends on the underlying runtime is considered to be
bad coding style, but sometimes such code is necessary to work around
runtime bugs. The supported way of detecting Mono is:

using System;

class Program {
static void Main ()
{
Type t = Type.GetType ("Mono.Runtime");
if (t != null)
Console.WriteLine ("You are running with the Mono VM");
else
Console.WriteLine ("You are running something else");
}
}

Any other hack, such as checking the underlying type of System.Int32
or of other corlib types, is doomed to fail in the future.

Long and short of it, just don't.



Related Topics



Leave a reply



Submit