首页 新闻 会员 周边

如何在ASP.NET Core中获取.NET Core Runtime的版本

0
悬赏园豆:30 [已解决问题] 解决于 2018-02-10 17:06

通过 PlatformServices.Default.Runtime.RuntimeVersion 获取的版本号不对,它得到的值是 4.0.0.0 。

问题补充:

RuntimeVersion的实现代码在这里

RuntimeVersion = typeof(object).GetTypeInfo().Assembly.GetName().Version.ToString();
dudu的主页 dudu | 高人七级 | 园豆:31007
提问于:2016-05-20 20:26
< >
分享
最佳答案
0

可以通过

Microsoft.Extensions.DependencyModel
DependencyContext.Default来获取
收获园豆:30
大肚小男人 | 菜鸟二级 |园豆:434 | 2018-02-10 16:14

的确可以,测试代码如下:

using Microsoft.Extensions.DependencyModel;
using System;
using System.Linq;

namespace Q82515
{
    class Program
    {
        static void Main(string[] args)
        {
            var dc = DependencyContext.Default;
            Console.WriteLine($"Framework: {dc.Target.Framework}");

            dc.RuntimeLibraries
            .Where(x => x.Name.Contains("Microsoft.NETCore.App"))
            .ToList()
            .ForEach(x =>
            {
                Console.WriteLine($"{x.Name} {x.Version}");
            });

            /*
            Output:
            Framework: .NETCoreApp,Version=v2.0
            Microsoft.NETCore.App 2.0.5
            runtime.win-x64.Microsoft.NETCore.App 2.0.5
            */
        }
    }
}
dudu | 园豆:31007 (高人七级) | 2018-02-10 17:04
其他回答(2)
0

最新版应该是1.0.0-preview1-002702,这个要翻源码了,没看到有介绍的。

CodeHsu | 园豆:5468 (大侠五级) | 2016-05-20 21:13

最新的.NET Core SDK的确是这个版本,我想把这个信息显示在ASP.NET Core的页面上

支持(0) 反对(0) dudu | 园豆:31007 (高人七级) | 2016-05-20 21:35

@dudu: 理解,去github上发个issues看看

支持(0) 反对(0) CodeHsu | 园豆:5468 (大侠五级) | 2016-05-20 22:26

@dudu: 晚上看了下 dotnet cli源码,里面用于显示dotnet cli 版本的代码如下:

private static string GetCommitSha()
{
    var versionFile = DotnetFiles.VersionFile;

    if (File.Exists(versionFile))
    {
        return File.ReadLines(versionFile).FirstOrDefault()?.Substring(0, 10);
    }

    return null;
}


namespace Microsoft.DotNet.Cli.Utils
{
    public static class DotnetFiles
    {
        private static string SdkRootFolder => Path.Combine(typeof(DotnetFiles).GetTypeInfo().Assembly.Location, "..");

        /// <summary>
        /// The CLI ships with a .version file that stores the commit information and CLI version
        /// </summary>
        public static string VersionFile => Path.GetFullPath(Path.Combine(SdkRootFolder, ".version"));
    }
}

其实就是读取文件内容,如下:

支持(0) 反对(0) CodeHsu | 园豆:5468 (大侠五级) | 2016-07-29 00:21
0

如果想要的是 .net Core Runtime ( CoreRT 本机工具链),代码如下:

            Process process = new Process
            {
                StartInfo = new ProcessStartInfo()
                {
                    Arguments = "--version",
                    CreateNoWindow = true,
                    FileName = "dotnet",
                    RedirectStandardOutput = true
                }
            };
            process.Start();
            process.WaitForExit();
            var version = process.StandardOutput.ReadToEnd().Trim();

如果想要的是.net core的版本, .net core 其实就是一堆类库,是以包的形式发布的。就像我们将项目中的package引用修改后,就从RC1变成RC2了。所以只需要确定引用包的版本就可以了,下面的代码检测.net core版本,而不是ASP.NET Core版本

            var configuration = new ConfigurationBuilder()
                .AddJsonFile("project.json").Build();
            return configuration.GetValue<string>("dependencies:Microsoft.NETCore.App:version");

 

下面这种方式不可用的原因是,它检查的是程序集的版本,而不是包的版本,两者可能相同也可能不同。

RuntimeVersion = typeof(object).GetTypeInfo().Assembly.GetName().Version.ToString();

虾。 | 园豆:257 (菜鸟二级) | 2016-07-11 16:23

 dotnet -v 获取到的是.NET Core SDK的版本

 configuration.GetValue<string>("dependencies:Microsoft.NETCore.App:version") 获取到的是.NET Core Runtime Framework的版本,有个更简单的获取方法:

PlatformServices.Default.Application.RuntimeFramework.FullName
支持(0) 反对(0) dudu | 园豆:31007 (高人七级) | 2016-07-11 16:34

@dudu: Framework版本是可以人为修改的,感觉不可靠。

支持(0) 反对(0) 虾。 | 园豆:257 (菜鸟二级) | 2016-07-11 17:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册