如何以编程方式找到

如何以编程方式找到

本文介绍了如何以编程方式找到.NET运行时的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个解决方案来为我提供完整框架的 .NET 运行时版本以及 .NET Core

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 Framework:

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(包括公开问题,来自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库。我怀疑是否会有一个好方法来获得给定执行程序集合背后的所有目标的 complete 图片...

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运行时的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:57