Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining .net framework version in a running application

(edited) Why does AssemblyName.Version report the same version even when I change the target version of my project? How can I determine the actual executing version of the framework?

This always returns 4.0.0.0

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            var aName = assembly.GetName();
            Console.WriteLine($"{aName.Name} {aName.Version}" );
        }
like image 215
Elroy Flynn Avatar asked Mar 22 '26 05:03

Elroy Flynn


1 Answers

Official example from Microsoft to detect .NET Framework runtime version is as below,

https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#net_d

using System;
using Microsoft.Win32;

public class GetDotNetVersion
{
   public static void Main()
   {
      GetDotNetVersion.Get45PlusFromRegistry();
   }

   private 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 >= 461808)
         return "4.7.2 or later";
      if (releaseKey >= 461308)
         return "4.7.1";
      if (releaseKey >= 460798)
         return "4.7";
      if (releaseKey >= 394802)
         return "4.6.2";
      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";
   }
}   
// This example displays output like the following:
//       .NET Framework Version: 4.6.1

Update: Turn out the true issue is related to the default set of SSL/TLS protocols that's used by .NET Framework classes, where if your assemblies are compiled against a certain framework version at compile time would trigger different runtime behaviors.

The AppContext switch is <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=true"/> as documented in KB3069494,

https://support.microsoft.com/en-us/help/3069494/cannot-connect-to-a-server-by-using-the-servicepointmanager-or-sslstre

You might also check the best practice article for more tips,

https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls?view=netframework-4.7.2

like image 53
Lex Li Avatar answered Mar 24 '26 22:03

Lex Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!