I wrote a C # program to detect the. NET Framework CLR version.
Running results in Ubuntu operating system:
./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 the Windows Vista operating system:
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, not the. NET Framework version. In fact, the following versions of the. NET Framework are installed in this Windows Vista operating system:
- 2.0.50727.4016
- 3.0.30729.4037
- 3.5.30729.01
The CLR versions of the three versions of the. NET Framework are all 2.0.50727.
Running results in the Windows Server 2003 operating system:
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
Additionally, the results of the ClrVer.exe in the Microsoft Windows SDK are:
ClrVer.exe Versions installed on the machine:v1.1.4322v2.0.50727v4.0.21006
Let's take a look at the source program. Here is ClrInfo.cs:
usingSystem;usingSystem.Text;namespaceskyiv{Public classClrinfo{static voidMain () {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(varFrameinchRuntimeframework.availableframeworks)Console. WriteLine (" " + frame); } }}
The following is RuntimeFramework.cs (the program was rewritten from a file of the same name in the source program of NUnit):
usingSystem;usingSystem.Reflection;usingSystem.Collections.Generic;usingMicrosoft.Win32;namespaceskyiv{Public enumRuntimeType{Any,//any supported runtime frameworkNet,//Microsoft. NET FrameworkNetCF,//Microsoft. NET Compact FrameworkSSCLI,//Microsoft Shared Source CLIMono,//Mono}//See http://nunit.org, this class from NUnit Project ' s RuntimeFramework.cs//Runtimeframework represents a Particu Lar version of a common language runtime implementation. [Serializable]Public Sealed classruntimeframework{ PublicRuntimeType Runtime {Get;Private Set; } PublicVersionVersion {Get;Private Set; }Public StringDisplayName {Get;Private Set; }StaticRuntimeframework currentframework;Public StaticRuntimeframework Currentframework {Get{if(Currentframework = =NULL) {varMonoruntimetype =Type. GetType ("Mono.runtime",false);varruntime = monoruntimetype! =NULL? RuntimeType.Mono:RuntimeType.Net; Currentframework =NewRuntimeframework (Runtime,Environment. Version);if(Monoruntimetype! =NULL) {varmethod = Monoruntimetype.getmethod ("GetDisplayName",BindingFlags. Static |BindingFlags. NonPublic |BindingFlags. DeclaredOnly |BindingFlags. exactbinding);if(Method! =NULL) Currentframework.displayname = (string) method. Invoke (NULL,New Object[0]); } }returnCurrentframework; } }Public StaticRuntimeframework[] Availableframeworks {Get{varFrameworks =NewList<RuntimeFramework> ();foreach(varFrameworkinchGetavailableframeworks (runtimetype.net)) frameworks. ADD (framework);foreach(varFrameworkinchGetavailableframeworks (Runtimetype.mono)) frameworks. ADD (framework);returnFrameworks. ToArray (); } }Public static BOOLIsmonoinstalled () {if(Currentframework.runtime = = Runtimetype.mono)return True;//Don ' t know how does this on the Linux yet, but it's not a problem since we is only supporting Mono on Linux if(Environment. Osversion.platform! =PlatformID. WIN32NT)return False;varKey =Registry. Localmachine.opensubkey (@ "Software\novell\mono");if(Key = =NULL)return False;varVersion = key. GetValue ("DEFAULTCLR")As string;if(string. IsNullOrEmpty (version))return False;returnKey. OpenSubKey (version)! =NULL; }//Returns An array of any available frameworks of a given type, for example, all mono or all. NET frameworks. Public StaticRuntimeframework[] Getavailableframeworks (RuntimeType RT) {varFrameworks =NewList<RuntimeFramework> ();if(RT = = Runtimetype.net &&Environment. Osversion.platform! =PlatformID. Unix) {varKey =Registry. Localmachine.opensubkey (@ "Software\microsoft\. Netframework\policy ");if(Key! =NULL)foreach(varNameinchKey. Getsubkeynames ())if(name. StartsWith ("V"))foreach(varBuildinchKey. OpenSubKey (name). GetValueNames ()) frameworks. ADD (NewRuntimeframework (RT,NewVersion(name. Substring (1) +"." + build)); }else if(RT = = Runtimetype.mono && ismonoinstalled ()) {varFramework =NewRuntimeframework (RT,NewVersion(1, 1, 4322)); Framework. DisplayName ="Mono 1.0 profile"; Frameworks. ADD (framework); Framework =NewRuntimeframework (RT,NewVersion(2, 0, 50727)); Framework. DisplayName ="Mono 2.0 Profile"; Frameworks. ADD (framework); }returnFrameworks. ToArray (); } PublicRuntimeframework (RuntimeType Runtime,VersionVersion) {runtime = runtime;Version= version; DisplayName = runtime. ToString () +" " + version. ToString (); }Public override StringToString () {returnDisplayName; } }}
In the Currentframework attribute of the Runtimeframework class, the static method of the Mono.runtime class is called through reflection if the current environment is found to be mono. GetDisplayName to set the DisplayName property.
In the static method Getavailableframeworks of the Runtimeframework class, if it is found that the current operating system is not Unix, it iterates through the software\microsoft\ of the registry. Netframework\policy item to detect the. NET Framework CLR version.
Below is makefile (for makefile, see: GUN make Chinese manual):
csc = cscSRC1 = ClrInfo.cs RuntimeFramework.csClrInfo.exe: $ (SRC1) $ (CSC)-out:[email protected] $ (SRC1)
You can compile with the make command under the Linux operating system. You can compile with the NMAKE command under the Windows operating system.
Note that Mono's C # compiler is GMCs, but she has an individual named CSC, as follows:
ls-l/usr/bin/csc/usr/bin/gmcslrwxrwxrwx 1 root root Cat/usr/bin/gmcsGMCs--version csc--version Mono C # compiler version 2.4.2.3
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
. NET Framework CLR Version detection