简体   繁体   English

确定正在运行的应用程序中的.net框架版本

[英]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? (已编辑)即使更改项目的目标版本,AssemblyName.Version为什么也报告相同的版本? How can I determine the actual executing version of the framework? 如何确定框架的实际执行版本?

This always returns 4.0.0.0 这总是返回4.0.0.0

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            var aName = assembly.GetName();
            Console.WriteLine($"{aName.Name} {aName.Version}" );
        }

Official example from Microsoft to detect .NET Framework runtime version is as below, Microsoft用来检测.NET Framework运行时版本的官方示例如下,

https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#net_d https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#ne​​t_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. 更新:真正的问题与.NET Framework类使用的默认SSL / TLS协议集有关,如果您的程序集在编译时针对某个框架版本进行编译,则会触发不同的运行时行为。

The AppContext switch is <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=true"/> as documented in KB3069494, <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=true"/>中所述,AppContext开关为<AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=true"/>

https://support.microsoft.com/en-us/help/3069494/cannot-connect-to-a-server-by-using-the-servicepointmanager-or-sslstre 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://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls?view=netframework-4.7.2

Try this: 尝试这个:

private static string GetExecutingAssemblyTargetFramework()
    {
        string result = string.Empty;
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        System.Runtime.Versioning.TargetFrameworkAttribute targetFrameworkAttribute = asm.GetCustomAttributes(
            typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false).FirstOrDefault()
            as System.Runtime.Versioning.TargetFrameworkAttribute;
        if (targetFrameworkAttribute != null)
            result = targetFrameworkAttribute.FrameworkDisplayName;
        return result;
    }

You can also use the same loop from the original question: 您还可以使用与原始问题相同的循环:

        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            Console.WriteLine(GetAssemblyTargetFramework(assembly));
        }

   private static string GetAssemblyTargetFramework(System.Reflection.Assembly asm)
    {
        string result = string.Empty;
        System.Runtime.Versioning.TargetFrameworkAttribute targetFrameworkAttribute = asm.GetCustomAttributes(
            typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false).FirstOrDefault()
            as System.Runtime.Versioning.TargetFrameworkAttribute;
        if (targetFrameworkAttribute != null)
            result = targetFrameworkAttribute.FrameworkDisplayName;
        return result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM