2

You can use System.Environment.Version to the get the version of the CLR; how do you get the versions of C# and of the .NET or Mono framework currently being used?

EDIT: I am NOT asking how to find whether you are using Mono or .NET. (The very first version of my question did ask that as a secondary question, but I deleted that almost immediately when I saw that question answered elsewhere.) The questions here are (1) how to figure out what version of C# you are working with, and (2) what version of .NET you have (as opposed to the version of the CLR, which isn't the same thing).

Ghopper21
  • 10,287
  • 10
  • 63
  • 92
  • Check this [link](http://stackoverflow.com/questions/951856/is-there-an-easy-way-to-check-net-framework-version-using-c) – andy Oct 17 '12 at 11:02
  • 3
    At *execution* time, there is no 'C#' being used... – AakashM Oct 17 '12 at 11:08
  • @AakashM -- ah right, the Python part of my brain was taking over there. Is there any C# preprocessor directive for checking the compiler version number? – Ghopper21 Oct 17 '12 at 11:32
  • [This](http://stackoverflow.com/questions/955623/how-can-compilation-of-c-sharp-code-be-made-to-require-a-given-language-or-compi) suggests it's not straightforward. – AakashM Oct 17 '12 at 11:54

2 Answers2

6

Checking if you're running in mono:

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

Checking the mono version:

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

If you're not in Mono, then you still use System.Environment.Version

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • 1
    Thanks. For .NET, `System.Environment.Version` is for the CLR version, not the .NET framework version, no? – Ghopper21 Oct 17 '12 at 11:30
0

a very similar question has been asked already:

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

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

and for detecting the .NET framework's version currently used at runtime for execution you can check Environment.Version

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147