2013-11-25 98 views
1

我應該如何在應用程序運行時檢測到在應用程序運行時檢測框架版本

在客戶端機器上安裝了什麼版本的.NET Framework?

+0

的可能重複[如何檢測安裝了哪些版本的.NET Framework和Service Pack?](http://stackoverflow.com/questions/199080/如何檢測what-net-framework-versions-and-service-packs) – Steve

回答

2
private static void GetVersionFromRegistry() 
{ 
    using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
     RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")) 
    { 
     foreach (string versionKeyName in ndpKey.GetSubKeyNames()) 
     { 
      if (versionKeyName.StartsWith("v")) 
      { 

       RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName); 
       string name = (string)versionKey.GetValue("Version", ""); 
       string sp = versionKey.GetValue("SP", "").ToString(); 
       string install = versionKey.GetValue("Install", "").ToString(); 
       if (install == "") //no install info, ust be later 
        Console.WriteLine(versionKeyName + " " + name); 
       else 
       { 
        if (sp != "" && install == "1") 
        { 
         Console.WriteLine(versionKeyName + " " + name + " SP" + sp); 
        } 

       } 
       if (name != "") 
       { 
        continue; 
       } 
       foreach (string subKeyName in versionKey.GetSubKeyNames()) 
       { 
        RegistryKey subKey = versionKey.OpenSubKey(subKeyName); 
        name = (string)subKey.GetValue("Version", ""); 
        if (name != "") 
         sp = subKey.GetValue("SP", "").ToString(); 
        install = subKey.GetValue("Install", "").ToString(); 
        if (install == "") //no install info, ust be later 
         Console.WriteLine(versionKeyName + " " + name); 
        else 
        { 
         if (sp != "" && install == "1") 
         { 
          Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp); 
         } 
         else if (install == "1") 
         { 
          Console.WriteLine(" " + subKeyName + " " + name); 
         } 

        } 

       } 

      } 
     } 
    } 
} 

資源信息: http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx

3
Environment.Version.ToString() 
+0

這將寫入公共語言運行庫的當前版本。如果他用2.0構建應用程序,他會得到什麼? –

+0

更正你的答案正確的他想要的應用程序運行時,而不是安裝哪個.net版本從我的+1 –

+0

這是有用的知道當前的應用程序環境,但課程無法檢測到哪些安裝在機器上。 @ User3805967澄清:與2.0它應該得到fw2.0,與3.0和3.5它應該得到3.0,並將應用程序設置爲4.0和/或4.5它會得到fw4.0,你的答案適應我的requeiments和工作不錯,我只是想接受你的答案,但你已經刪除了它,謝謝你 – pitoloko

相關問題