问题描述
我需要一个解决方案来为我提供完整框架和 .NET Core 的 .NET 运行时版本.
I need a solution to give me the .NET run-time version of both the full framework as well as the .NET Core.
在具有以下 .NET 版本的机器上:
On a machine with the following .NET versions:
Full: 4.7.2
Core: 2.1.104
运行:
RuntimeInformation.FrameworkDescription
给我:
Full: .NET Framework 4.7.2558.0
Core: .NET Core 4.6.26212.01
运行:
Environment.Version
给我:
Full: 4.0.30319.42000
Core: 4.0.30319.42000
知道注册表选项在 Windows 之外不可用,我如何才能准确地获得跨不同平台的运行时版本.
How can I accurately get the run-time versions across different platforms knowing that the registry option is not available outside Windows.
推荐答案
目前还没有统一的方法来做到这一点,尽管有一个公开的请求 此处 可以跟踪.如果您单击引用该讨论的各种问题以及下游进一步引用的问题,您会发现现在某些实现中也存在一些错误,但有一些积极的工作(其中一个问题有相关的签入)8 小时前).
There isn't a unified way to do this yet, although there is an open request for this here that you can track. If you click through the various issues that reference that discussion and the issues referenced further downstream, you'll see there are also some bugs in some implementations right now, but there is active work (one of the issues had a related check-in just 8 hours ago).
对于 .NET 框架:
For .NET Framework:
using System;
...
string ver = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
对于 .NET Core:
and for .NET Core:
using System.Reflection;
using System.Runtime.Versioning;
...
string ver = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
输出:
.NETFramework,Version=v4.5.1
.NETCoreApp,Version=v2.0
显然这些以编程方式使用有点麻烦,因此要求更好的 API(包括 this 来自 Microsoft 的未决问题,讨论了专门针对最小目标框架测试的新 API).
Obviously these are a bit of a hassle to use programmatically, hence the requests for a better API (including this open issue from Microsoft discussing a new API specifically focused on testing for a minimum target framework).
另一个可能永远不可能的难题是给定的应用程序可以引用许多目标.例如,在幕后,您可能会引入 .NET Standard 2.x 和 .NET Standard 1.x 库.我怀疑永远不会有一个很好的方法来获得一个给定的执行程序集集合背后的所有目标的完整图片......
The other piece of the puzzle that will probably always be impossible is that a given application can reference many targets. Under the hood you might be pulling in .NET Standard 2.x and .NET Standard 1.x libraries, for example. I doubt there will ever be a good way to get a complete picture of all the targets behind a given collection of executing assemblies...
这篇关于如何以编程方式找到 .NET 运行时的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!