【问题标题】:.NET Core current version.NET Core 当前版本
【发布时间】:2018-11-11 09:57:39
【问题描述】:

如何确定我的应用在哪个版本的 .NET Core 中运行?

我试图从 IHostingEnvironment(还有 Startup.cs 和 Program.cs)中确定这一点,但没有成功。

这个问题:Get Current .NET CLR version at runtime? 与 .NET Framework 有关。我的问题是关于 .NET Core。

【问题讨论】:

    标签: .net asp.net-core version


    【解决方案1】:

    您可以从Microsoft.Extensions.PlatformAbstractions 下的PlatformServices.Default.Application.RuntimeFramework 属性获取运行时版本。

    Program.cs:

    Console.WriteLine(PlatformServices.Default.Application.RuntimeFramework);
    

    更新: 根据这个aspnet/AnnouncementMicrosoft.Extensions.PlatformAbstractions已经被去掉了,所以RuntimeFramework要替换成:

    Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName);
    

    【讨论】:

      【解决方案2】:

      其中一种快捷方式是转到菜单栏并选择Project-&gt;Properties-&gt;Application

      然后你会看到你的项目使用的目标框架版本是什么。

      【讨论】:

        【解决方案3】:

        如果不希望使用using System.Reflection;,则可以使用GetCustomAttributes

        static string GetFrameworkName()
                    => ((System.Runtime.Versioning.TargetFrameworkAttribute)
                            (System.Reflection.Assembly.GetEntryAssembly()
                            .GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), true)[0]))
                            .FrameworkName; // Example: .NETCoreApp,Version=v3.0
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多