如何获取应用程序正在使用的.NET框架版本

时间:2013-02-28 05:53:30

标签: c#

我已经提到了Stack Overflow问题 Is there an easy way to check the .NET Framework version? 。但是那里提出的建议不符合以下目的。

我们如何识别C#控制台应用程序正在使用的.NET版本?

环境:

  • Visual Studio 2010
  • .NET Framework:3.5(请参阅随附的屏幕截图)

CODE

using System;
using System.Globalization;
using Microsoft.Win32;
namespace TESTConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //.NET version: Approach 1
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
            string[] version_names = installed_versions.GetSubKeyNames();
            double latestFramework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);
            int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));
            Console.WriteLine(latestFramework);

            //Approach 2
            string versionval = Environment.Version.ToString();
            Console.WriteLine(versionval);

            //Approach 3
            string systemVersionVal = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion().ToString();
            Console.WriteLine(systemVersionVal);

            Console.ReadLine();
        }
    }
}

输出

Enter image description here

版本设置

Enter image description here

1 个答案:

答案 0 :(得分:2)

接近的第二个和第三个是CLR version numbers

.NET Framework 2和.NET Framework 3.5正在使用CLR 2.0

并且没有CLR 3.0或3.5。