.NET Framework CLR 版本检测

我写了一个 C# 程序来检测 .NET Framework CLR 版本。

在 Ubuntu 操作系统中的运行结果:

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

在 Windows Vista 操作系统中的运行结果:

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

注意,这个程序检测的是 .NET Framework CLR 版本,而不是 .NET Framework 版本。实际上这个 Windows Vista 操作系统中安装了 .NET Framework 的以下版本:

而三个版本的 .NET Framework 的 CLR 版本都是 2.0.50727。

在 Windows Server 2003 操作系统中的运行结果:

E:\CS\ClrInfo> ClrInfo.exeOS Version: Microsoft Windows NT 5.2.3790 Service Pack 2CLR 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

另外,,Microsoft Windows SDK中的 ClrVer.exe 的运行结果:

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

我们来看看源程序吧。下面是 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);} }}

下面是 RuntimeFramework.cs (该程序是从NUnit的源程序中的同名文件改写而来的):

using System;using System.Reflection;using System.Collections.Generic;using Microsoft.Win32;namespace Skyiv{ public enum RuntimeType {Any, // Any supported runtime frameworkNet, // Microsoft .NET FrameworkNetCF, // Microsoft .NET Compact FrameworkSSCLI, // Microsoft Shared Source CLIMono, // Mono } // See , 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<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 Linuxif (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;} }}为了一些琐事吵架,然后冷战,疯狂思念对方,最后和好。

.NET Framework CLR 版本检测

相关文章:

你感兴趣的文章:

标签云: