locked
how to get app runtime .net framework version RRS feed

  • Question

  • in app.config:

    <startup> <supportedRuntime version="v4.0" sku=".NETFramewrok,Version=v4.5.2"/> </startup>

    in my application, I tried to read the runtime .net framework version with 2 ways:

    1. System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion().ToString(): it only return 4.0.30319.

    2. Enviroment.Version: it always gives 4.0.30319.42000

    Neither of them worked.

    How can we identify runtime .net framework version?

    Wednesday, December 7, 2016 12:43 AM

Answers

  • The link I quoted for you even has code embedded that you can just copy and paste...

    using System;
    using Microsoft.Win32;
    
    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. A non-null release key should mean
       	// 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
    P.S.: Seems the syntax highlight feature in this forum does not know how to handle "@" quoted string literals

    Wednesday, December 7, 2016 2:23 AM
    Answerer

All replies

  • See the "Warning" part in "Remark" section of the documentation:

    <blockquote>

    For the .NET Framework 4.5 and later, we do not recommend using the Version property to detect the version of the runtime; instead, you can determine the version of the common language runtime by querying the registry. For more information, see How to: Determine Which .NET Framework Versions Are Installed.

    </blockquote>

    Wednesday, December 7, 2016 1:44 AM
    Answerer
  • if querying the registry, it can only get current installed .net framework versions, but we want to get application's runtime .netframework version, for example, my pc installed 4.5.2, but if we set <startup> <supportedRuntime version="v4.0" sku=".NETFramewrok,Version=v4.5"/> </startup> in my application's app.config, we want to get 4.5 version.
    Wednesday, December 7, 2016 2:10 AM
  • The link I quoted for you even has code embedded that you can just copy and paste...

    using System;
    using Microsoft.Win32;
    
    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. A non-null release key should mean
       	// 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
    P.S.: Seems the syntax highlight feature in this forum does not know how to handle "@" quoted string literals

    Wednesday, December 7, 2016 2:23 AM
    Answerer
  • Btw, as the .NET team are moving to use .NET standard version to replace runtime versions, and use monthly update to replace SPs, checking for runtime version number could be pointless at major scale (language features) and minor scale (system bug fixes) shortly thereafter.
    Wednesday, December 7, 2016 4:47 AM
    Answerer
  • Hi Kelly,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, December 13, 2016 11:15 AM
  • Hi Kelly,

    Any update?


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, December 19, 2016 3:18 PM