2011-02-23 77 views
6

如何从c#应用程序本身检查应用程序正在使用哪个dotnet版本?确定我的dotnet版本

+0

您的意思是应用程序所需的最小版本? (项目文件中的TargetFrameworkVersion属性) – 2011-02-23 10:49:00

+0

其实我需要当前应用程序框架的路径。我的应用程序需要从当前运行的版本执行一些exe。即如果我的应用程序使用2.0,它将从.net 2.0文件夹中选择exe,如果使用4,它将从.net 4.0文件夹中选择exe。 – hungryMind 2011-02-23 12:17:35

回答

12

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

获取版本对象,该对象描述公共语言运行库的主要版本,次要版本,生成版本和修订版本号。


要找出安装的是什么版本的框架,见this SO问题和答案。在坚果外壳中,您将需要深入注册表。

+3

它可以区分3.5,3.0和2.0吗? (运行时是相同的,2.0.something) – xanatos 2011-02-23 10:52:36

+0

@xanatos - 不,它不能。它将返回运行时版本。如果您需要知道安装的内容,请参阅[本](http://stackoverflow.com/questions/199080/how-to-detect-what-net-framework-versions-and-service-packs-are-installed)所以问题和答案。 – Oded 2011-02-23 11:14:14

6

您可以使用:

Environment.Version 

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

0

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

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 
} 
+0

添加一些解释与答案如何这个答案帮助OP在解决当前问题 – 2016-10-07 08:58:15

0

在你的Visual Studio, 转到工具> Nutget包管理 - >软件包管理器控制台 键入DOTNET --version 在这里你走了!