2013-02-28 18 views
4

我已經提到堆棧溢出問題Is there an easy way to check the .NET Framework version?。但是那裏給出的建議沒有用於以下目的。如何獲得應用程序正在使用的.NET框架版本

我們如何識別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

VERSION設置

Enter image description here

+0

@Habib對不起,那個 – 1Mayur 2013-02-28 06:00:51

+2

這有助於http://stackoverflow.com/a/8543850/284111? – 2013-02-28 06:03:01

+0

你需要更具體的什麼「版本」你需要 - 與建設3.5你得到運行時2.0或4.0。這可能是你需要的,但澄清會有幫助。 – 2013-02-28 06:21:04

回答

4

嘗試此(它會爲版本一起> = 3.5)

string version = System.Reflection.Assembly 
        .GetExecutingAssembly() 
        .GetReferencedAssemblies() 
        .Where(x => x.Name == "System.Core").First().Version.ToString(); 

P.S.愚蠢的回答從我的舊年,請忽略此(我不能刪除這個,因爲它是一個被接受的答案)

+3

這不適用於.net framework 2.0,因爲它裏面沒有「System.Core」(不要LINQ)。 – 2013-02-28 06:10:56

+0

是的,我正在試圖解決這個問題。 – 2013-02-28 06:11:35

+0

所以基本的想法是檢查'System.Core dll'的版本號?你能否詳細說明答案中的邏輯? – Lijo 2013-02-28 07:00:05

2

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

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

而且沒有CLR 3.0或3.5。

+1

這......除非你加載WCF庫,否則我不是肯定會說什麼... – JerKimball 2013-02-28 07:09:58

相關問題