3.5.30729.01The 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