I wrote a C #ProgramTo detect the. NET Framework CLR version.
Running results in Ubuntu:
Ben@ben-m4000t :~ /Work $./Clrinfo.exeOS 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: \ CS \ clrinfo>Clrinfo.exeOS 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: \ CS \ clrinfo>Clrinfo.exeOS 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. 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: \ CS \ clrinfo>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 particle 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 <Runtimeframework> (); 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 ( @ "SOFTWARE \ Novell \ mono" ); 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 <Runtimeframework> (); If (RT = runtimetype. Net && Environment . Osversion. platform! = Platformid . UNIX ){ VaR Key =Registry . Localmachine. opensubkey ( @ "SOFTWARE \ Microsoft \. netframework \ Policy" ); 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 ;}}}
InRuntimeframeworkIf the current environment is mono in the currentframework attribute of the class, it is called through reflection.Mono. RuntimeClass static method getdisplayname to set the displayname attribute.
InRuntimeframeworkIf you find that the current operating system is not UNIX in getavailableframeworks, You can traverse the SOFTWARE \ Microsoft \. netframework \ policy 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 4 2009-11-01 18:53/usr/bin/CSC-> GMCs-rwxr-XR-x 1 Root 75/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
Finally, the complete source code can be downloaded on the http://bitbucket.org/ben.skyiv/clrinfo/downloads/ page.
You can also download it using the Hg clone http://bitbucket.org/ben.skyiv/clrinfo/ command.
For more information about HG, see the mercurial memorandum.