. NET Framework CLR version check

Source: Internet
Author: User

. NET Framework CLR version check

I wrote a C # program to check the. NET Framework CLR version.

Running results in Ubuntu:

ben@ben-m4000t:~/work$ ./ClrInfo.exe      OS Version: Unix 2.6.31.16     CLR Version: 2.0.50727.1433  ( Mono 2.4.2.3 )Default Encoding: System.Text.UTF8EncodingAvailable Frameworks:  Mono 1.0 Profile  Mono 2.0 Profile

Running results in Windows Vista:

E:CSClrInfo> ClrInfo.exe      OS Version: Microsoft Windows NT 6.0.6002 Service Pack 2     CLR Version: 2.0.50727.4200  ( Net 2.0.50727.4200 )Default Encoding: System.Text.DBCSCodePageEncodingAvailable Frameworks:  Net 2.0.50727

Note that this program detects the. NET Framework CLR version instead of the. NET Framework version. In fact, the following. NET Framework versions are installed in the Windows Vista operating system:

  • 2.0.50727.4016
  • 3.0.30729.4037
  • 3.5.30729.01

    The CLR versions of the. NET Framework of the three versions are 2.0.50727.

    Running results in Windows Server 2003:

    E:CSClrInfo> ClrInfo.exe      OS Version: Microsoft Windows NT 5.2.3790 Service Pack 2     CLR Version: 2.0.50727.3603  ( Net 2.0.50727.3603 )Default Encoding: System.Text.DBCSCodePageEncodingAvailable Frameworks:  Net 1.1.4322  Net 2.0.50727  Net 4.0.21006

    In addition, the running result of ClrVer.exe in Microsoft Windows SDK is as follows:

    E:CSClrInfo> ClrVer.exeVersions installed on the machine:v1.1.4322v2.0.50727v4.0.21006

    Let's take a look at the source program. The following is ClrInfo. cs:

    using System;using System.Text;namespace Skyiv{  public class ClrInfo  {    static void Main()    {      Console.WriteLine(      OS Version: {0}, Environment.OSVersion);      Console.WriteLine(     CLR Version: {0}  ( {1} ), Environment.Version, RuntimeFramework.CurrentFramework);      Console.WriteLine(Default Encoding: {0}, Encoding.Default);      Console.WriteLine();      Console.WriteLine(Available Frameworks:);      foreach (var frame in RuntimeFramework.AvailableFrameworks) Console.WriteLine(   + frame);    }  }}

    The following is RuntimeFramework. cs (this program is rewritten from a file with the same name in the NUnit source program ):

    using System;using System.Reflection;using System.Collections.Generic;using Microsoft.Win32;namespace Skyiv{  public enum RuntimeType  {    Any,   // Any supported runtime framework    Net,   // Microsoft .NET Framework    NetCF, // Microsoft .NET Compact Framework    SSCLI, // Microsoft Shared Source CLI    Mono,  // Mono  }  // See http://nunit.org, this class from NUnit Project's RuntimeFramework.cs  // RuntimeFramework represents a particular version of a common language runtime implementation.  [Serializable]  public sealed class RuntimeFramework  {    public RuntimeType Runtime { get; private set; }    public Version Version { get; private set; }    public string DisplayName { get; private set; }    static RuntimeFramework currentFramework;    public static RuntimeFramework CurrentFramework    {      get      {        if (currentFramework == null)        {          var monoRuntimeType = Type.GetType(Mono.Runtime, false);          var runtime = monoRuntimeType != null ? RuntimeType.Mono : RuntimeType.Net;          currentFramework = new RuntimeFramework(runtime, Environment.Version);          if (monoRuntimeType != null)          {            var method = monoRuntimeType.GetMethod(GetDisplayName, BindingFlags.Static |              BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding);            if (method != null) currentFramework.DisplayName = (string)method.Invoke(null, new object[0]);          }        }        return currentFramework;      }    }    public static RuntimeFramework[] AvailableFrameworks    {      get      {        var frameworks = new List
       
        ();        foreach (var framework in GetAvailableFrameworks(RuntimeType.Net)) frameworks.Add(framework);        foreach (var framework in GetAvailableFrameworks(RuntimeType.Mono)) frameworks.Add(framework);        return frameworks.ToArray();      }    }    public static bool IsMonoInstalled()    {      if (CurrentFramework.Runtime == RuntimeType.Mono) return true;      // Don't know how to do this on linux yet, but it's not a problem since we are only supporting Mono on Linux      if (Environment.OSVersion.Platform != PlatformID.Win32NT) return false;      var key = Registry.LocalMachine.OpenSubKey(@SoftwareNovellMono);      if (key == null) return false;      var version = key.GetValue(DefaultCLR) as string;      if (string.IsNullOrEmpty(version)) return false;      return key.OpenSubKey(version) != null;    }    // Returns an array of all available frameworks of a given type, for example, all mono or all .NET frameworks.    public static RuntimeFramework[] GetAvailableFrameworks(RuntimeType rt)    {      var frameworks = new List
        
         ();      if (rt == RuntimeType.Net && Environment.OSVersion.Platform != PlatformID.Unix)      {        var key = Registry.LocalMachine.OpenSubKey(@SoftwareMicrosoft.NETFrameworkpolicy);        if (key != null)          foreach (var name in key.GetSubKeyNames())            if (name.StartsWith(v))              foreach (var build in key.OpenSubKey(name).GetValueNames())                frameworks.Add(new RuntimeFramework(rt, new Version(name.Substring(1) + . + build)));      }      else if (rt == RuntimeType.Mono && IsMonoInstalled())      {        var framework = new RuntimeFramework(rt, new Version(1, 1, 4322));        framework.DisplayName = Mono 1.0 Profile;        frameworks.Add(framework);        framework = new RuntimeFramework(rt, new Version(2, 0, 50727));        framework.DisplayName = Mono 2.0 Profile;        frameworks.Add(framework);      }      return frameworks.ToArray();    }    public RuntimeFramework(RuntimeType runtime, Version version)    {      Runtime = runtime;      Version = version;      DisplayName = runtime.ToString() +   + version.ToString();    }    public override string ToString()    {      return DisplayName;    }  }}
        
       

    In the CurrentFramework attribute of the RuntimeFramework class, if the current environment is mono, call the static method GetDisplayName of the Mono. Runtime class through reflection to set the DisplayName attribute.

    In the static method GetAvailableFrameworks of the RuntimeFramework class, if you find that the current operating system is not Unix, You can traverse the SoftwareMicrosoft. NETFrameworkpolicy item of the Registry to detect the. NET Framework CLR version.

    The following is makefile (for more information about makefile, see the GUN make Chinese manual ):

    CSC = cscSRC1 = ClrInfo.cs RuntimeFramework.csClrInfo.exe: $(SRC1)$(CSC) -out:$@ $(SRC1)

    In Linux, you can use the make command to compile. In Windows, you can use the nmake command to compile.

    Note that Mono's C # compiler is gmcs, but she has a few names named csc, as shown below:

    ben@ben-m4000t:~$ ls -l /usr/bin/csc /usr/bin/gmcslrwxrwxrwx 1 root root  4 2009-11-01 18:53 /usr/bin/csc -> gmcs-rwxr-xr-x 1 root root 75 2009-09-23 23:04 /usr/bin/gmcsben@ben-m4000t:~$ cat /usr/bin/gmcs#!/bin/shexec /usr/bin/mono $MONO_OPTIONS /usr/lib/mono/2.0/gmcs.exe $@ben@ben-m4000t:~$ gmcs --versionMono C# compiler version 2.4.2.3ben@ben-m4000t:~$ csc --versionMono C# compiler version 2.4.2.3

     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.