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

7
我参考了Stack Overflow上的问题(有没有一种简单的方法来检查.NET Framework版本?),但是那里给出的建议对以下目的不起作用。
我们如何确定C#控制台应用程序正在使用的.NET版本?
环境:
Visual Studio 2010
.NET Framework:3.5(请参见附加的屏幕截图)
代码
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


@Habib 对此感到抱歉。 - 1Mayur
2
这个有帮助吗?https://dev59.com/52oy5IYBdhLWcg3wfeER#8543850? - Andrew Savinykh
你需要更具体地说明你需要的"版本"——使用3.5进行构建将获取2.0或4.0的运行时。这可能是你所需的,但明确一点会更有帮助。 - Alexei Levenkov
1个回答

2
第二和第三种方法是“CLR版本号”。
.NET Framework 2和.NET Framework 3.5使用的是“CLR 2.0”。
没有CLR 3.0或3.5。

1
这个...除非你正在加载WCF库,否则我不确定它会说什么... - JerKimball

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接