【问题标题】:Identify my dotnet version [duplicate]确定我的 dotnet 版本 [重复]
【发布时间】:2011-02-23 10:43:07
【问题描述】:

如何从 c# 应用程序本身检查应用程序使用的 dotnet 版本是什么?

【问题讨论】:

  • 您是指应用程序所需的最低版本吗? (项目文件中的TargetFrameworkVersion attr)
  • 其实我需要当前应用框架的路径。我的应用程序需要从当前运行的版本执行一些 exe。即,如果我的应用程序使用 2.0,它将从 .net 2.0 文件夹中选择 exe,如果使用 4,它将从 .net 4.0 文件夹中选择 exe。

标签: c# .net


【解决方案1】:

使用Environment.Version - 它为您提供运行应用程序的确切版本的 .NET。

获取描述公共语言运行时的主要、次要、内部版本和修订号的版本对象。


要了解安装的框架版本,请参阅this SO 问题和答案。简而言之,您需要深入研究注册表。

【讨论】:

  • 能区分3.5、3.0、2.0吗? (运行时相同,2.0.something)
  • @xanatos - 不,它不能。它将返回运行时版本。如果您需要知道安装了什么,请参阅this SO question and answers。
【解决方案2】:

您可以使用:

Environment.Version

获取 .NET 运行时的版本号。

【讨论】:

    【解决方案3】:

    创建一个控制台应用程序添加这个类并运行它

     using Microsoft.Win32;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
    
        namespace ConsoleApplication2
        {
            public class GetDotNetVersion
            {
                public static void Get45PlusFromRegistry()
        {
            const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
            using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
            {
                if (ndpKey != null && ndpKey.GetValue("Release") != null)
                {
                    Console.WriteLine(".NET Framework Version: " +  CheckFor45PlusVersion((int)ndpKey.GetValue("Release")));
                }
                else
                {
                    Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
                }
            }
        }
    
        // Checking the version using >= will enable forward compatibility.
        private static string CheckFor45PlusVersion(int releaseKey)
        {
            if (releaseKey >= 394802)
                return "4.6.2 or later";
            if (releaseKey >= 394254)
            {
                return "4.6.1";
            }
            if (releaseKey >= 393295)
            {
                return "4.6";
            }
            if ((releaseKey >= 379893))
            {
                return "4.5.2";
            }
            if ((releaseKey >= 378675))
            {
                return "4.5.1";
            }
            if ((releaseKey >= 378389))
            {
                return "4.5";
            }
            // This code should never execute. 
            // that 4.5 or later is installed.
            return "No 4.5 or later version detected";
        }
     }
     // Calling the GetDotNetVersion.Get45PlusFromRegistry method produces 
     // output like the following:
     //       .NET Framework Version: 4.6.1
     }
    

    【讨论】:

    • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
    【解决方案4】:

    在您的 Visual Studio 中, 转到工具-> Nutget 包管理-> 包管理器控制台 输入 dotnet --version 给你!

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2022-06-16
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      相关资源
      最近更新 更多