在运行中的应用程序中确定 .net framework 版本

3

(编辑) 为什么AssemblyName.Version报告的版本即使我改变了项目的目标版本仍然相同? 如何确定框架的实际执行版本?

这总是返回4.0.0.0

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

1

微软官方提供的检测.NET Framework运行时版本的示例如下:

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

更新: 事实证明真正的问题与.NET Framework类使用的默认SSL/TLS协议集相关,如果您的程序集在编译时针对某个框架版本进行编译会触发不同的运行时行为。

AppContext开关是<AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=true"/>,如KB3069494所述。

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

你可以查看最佳实践文章获取更多提示,

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


2
这并没有回答我所问的问题。它回答了“哪些版本已安装”的问题。我所问的问题是“当前应用程序使用的是哪个版本?”请注意我发布的第一句话中的斜体字。 - Elroy Flynn
2
那么,当我将应用程序的目标从4.5更改为4.6并在同一台机器上运行时,为什么(其他部分)我的应用程序会产生不同的结果呢?正如所指出的那样,我更改了目标版本,就会得到不同的行为。 - Elroy Flynn
那就是你的真正问题。微软设计了兼容层来伪装你的应用程序在旧版本.NET Framework上运行,这在文档中已经广为人知,比如https://learn.microsoft.com/en-us/dotnet/framework/whats-new/#whats-new-in-net-2015(“兼容性开关”部分)。根据你观察到的行为差异,有相应的方法来解决它们。 - Lex Li
我想我找到了开关,所以刚刚更新了答案。 - Lex Li
让我们在聊天中继续这个讨论 - Elroy Flynn
显示剩余3条评论

-1

试试这个:

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;
    }

你也可以使用原始问题中的相同循环:

        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;
    }

1
OP不是正在寻找TargetFramework,他正在寻找当前执行的框架版本。目标框架是指该程序集是针对哪个框架构建的指示器,而不是当前执行程序集的框架。 - enorl76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接