如何在运行时检测 .NET 4.5 版当前正在运行您的代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8517159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I detect at runtime that .NET version 4.5 is currently running your code?
提问by Evereq
I installed .NET 4.5 Developer preview from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27541, which 'replaces' .NET 4.0 version.
我从http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27541安装了 .NET 4.5 Developer preview ,它“替换”了 .NET 4.0 版本。
However, the old way to detect the .NET framework version seems to return 4.0 (more precisely 4.0.30319.17020 on my PC), instead of 4.5 (sure probably for backward compatibility, or?):
但是,检测 .NET 框架版本的旧方法似乎返回 4.0(在我的 PC 上更准确地说是 4.0.30319.17020),而不是 4.5(当然可能是为了向后兼容,还是?):
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var version = Environment.Version;
Console.WriteLine(version.ToString());
Console.ReadKey();
}
}
}
How do I detect that my code is really executed by .NET 4.5?
如何检测我的代码是否真的由 .NET 4.5 执行?
回答by Christian.K
You need to make a clear distinction between the CLR (i.e. the "runtime") and the framework libraries (i.e. the "framework"). You execute your code on or with the first, your code is compiled against and uses the later. Unfortunately when using the the term ".NET version" one is usually referring to the whole package of both runtime and framework, regardless of their individual versions which - as has been said - can differ.
您需要明确区分 CLR(即“运行时”)和框架库(即“框架”)。您在第一个上执行代码或使用第一个执行代码,您的代码针对后者进行编译并使用后者。不幸的是,当使用术语“.NET版本”时,通常指的是运行时和框架的整个包,而不管它们各自的版本 - 正如所说 - 可以不同。
You can detect the installed framework versions. That doesn't, however, tell you which one you are actually using at runtime.
您可以检测已安装的框架版本。但是,这并不能告诉您在运行时实际使用的是哪一个。
I'm not sure about 4.5, but with 2.0 vs. 3.0 or 3.5 Environment.Versionwas of no help since it always returned 2.0 as all those framework versions were using the CLR 2.0. I presumethat with the framework4.5 the CLR version is still 4.0, which would explain that Environment.Versionreturns 4.0.x even in that case.
我不确定 4.5,但 2.0 对 3.0 或 3.5Environment.Version没有帮助,因为它总是返回 2.0,因为所有这些框架版本都使用 CLR 2.0。我认为对于框架4.5,CLR 版本仍然是 4.0,这可以解释Environment.Version即使在这种情况下也会返回 4.0.x。
A technique that may work for you is to check for a type, method or property in the core libraries (mscorlib, System.Core, etc.) that you'd know only existed starting with a particular .NET framework version.
一种可能对您有用的技术是检查核心库(mscorlib、System.Core 等)中的类型、方法或属性,您知道这些库仅从特定的 .NET 框架版本开始就存在。
For example, the ReflectionContextclass seems to be totally new with the .NET framework 4.5 and conveniently lives in mscorlib. So you could do something like this.
例如,ReflectionContext类在 .NET framework 4.5 中似乎是全新的,并且方便地位于mscorlib. 所以你可以做这样的事情。
public static bool IsNet45OrNewer()
{
// Class "ReflectionContext" exists from .NET 4.5 onwards.
return Type.GetType("System.Reflection.ReflectionContext", false) != null;
}
Having all that said, one could question why you need to know which .NET version you are using. Simply try to access the features you require and possibly gracefully fallback to something else (that was available in older versions) if they are not present.
说了这么多,人们可能会质疑为什么您需要知道您使用的是哪个 .NET 版本。只需尝试访问您需要的功能,如果它们不存在,可能会优雅地回退到其他功能(旧版本中可用)。
Update: Note that the term .NET 4.5 refers to the whole package of several assemblies that make up the base class libraries (BCL) and more (collectively called "framework") plus the runtime itself, i.e. the CLR - both of which can have different versions, as has been said.
更新:请注意,术语 .NET 4.5 是指构成基类库 (BCL) 和更多(统称为“框架”)的几个程序集的整个包以及运行时本身,即 CLR - 两者都可以具有如前所述,不同的版本。
I don't work for Microsoft and have no insight into the real reasons behind the lack of a (single) function or API to get "the .NET framework version", but I can make an educated guess.
我不在 Microsoft 工作,也不了解缺少(单个)函数或 API 来获取“.NET 框架版本”背后的真正原因,但我可以做出有根据的猜测。
It is not clear what information in particular such a function/API should provide.Even the individual assemblies of the BCL don't share a common (assembly/file) version. For example, with .NET 3.0 and 3.5, the mscorlib.dll had version 2.0.x while only the new assemblies of WCF and WF had 3.0 something. I thinkeven with .NET 3.5 the
System.ServiceModel.dllstill had version 3.0.x. What I'm trying to say, there is no unified version on all assemblies of the framework. So what should an API call like, say,System.Environment.FrameworkVersionreturn? What value would the version be (even if it did return the "symbolic" version like 4.5, it would be of little value, wouldn't it?).Too specificSome new features might arrive in SPs to existing versions, plus being part of a new version. Doing feature checking, your application might run perfectly well on previousversions, that have been updated, while with explicit version checking it might unneccessarily restrict itself to the newest version. I don't have an example from the .NET world for this, but in general (and in Windows itself, e.g. WMI) it can and did happen.
Not wanted.I could imagine that a method to figure "the version of the framework" that is currently being used by an application is not even desirablefor them to provide. There is a long and unholy history of version checking fallacies(see "Don't Check Version" paragraph for concepts that apply to .NET as well), in the native/Win32 world. For example, people used the
GetVersionandGetVersionExAPIs wrong, by only checking that they run the version they knew was the newest when they wrote their application. So, when the application was run on a newer version of windows, it wouldn't run, even though the featuresthey really were using are still there. Microsoft might have considered issues like that and thus not even provided some API in .NET.
目前尚不清楚此类功能/API 应提供哪些信息。甚至 BCL 的各个程序集也不共享通用(程序集/文件)版本。例如,在 .NET 3.0 和 3.5 中,mscorlib.dll 的版本为 2.0.x,而只有 WCF 和 WF 的新程序集具有 3.0 版本。我认为即使使用 .NET 3.5
System.ServiceModel.dll仍然有 3.0.x 版本。我想说的是,框架的所有程序集都没有统一的版本。那么 API 调用应该System.Environment.FrameworkVersion返回什么?该版本有什么价值(即使它确实返回了像 4.5 这样的“符号”版本,它也没有什么价值,不是吗?)。过于具体某些新功能可能会出现在 SP 中的现有版本,并且是新版本的一部分。进行功能检查时,您的应用程序可能会在已更新的先前版本上运行良好,而通过显式版本检查,它可能会不必要地将自身限制为最新版本。我没有来自 .NET 世界的例子,但总的来说(以及在 Windows 本身,例如 WMI)它可以并且确实发生了。
不想要。我可以想象,他们甚至不希望提供一种计算应用程序当前正在使用的“框架版本”的方法。在本机/Win32 世界中,版本检查谬误(参见“不要检查版本”段落以了解也适用于 .NET 的概念)的历史悠久而邪恶。例如,人们错误地使用API
GetVersion和GetVersionExAPI,只检查他们运行的版本是否是他们在编写应用程序时知道的最新版本。因此,当应用程序在较新版本的 Windows 上运行时,它不会运行,即使功能他们真的在使用仍然存在。Microsoft 可能已经考虑过类似的问题,因此甚至没有在 .NET 中提供一些 API。
Incidentally this is what Microsoft recommendsin the remarks section of the GetVersion function:
顺便说一下,这是微软在 GetVersion 函数的备注部分推荐的:
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.
识别当前操作系统通常不是确定是否存在特定操作系统功能的最佳方法。这是因为操作系统可能在可再发行 DLL 中添加了新功能。与其使用 GetVersionEx 来确定操作系统平台或版本号,不如测试功能本身是否存在。
The Windows Team Blog also has something to sayabout this.
Windows 团队博客对此也有话要说。
I know all this was about Windows and native programming, but the concept and dangers are the same for .NET applications the framework and the CLR.
我知道这一切都与 Windows 和本机编程有关,但对于 .NET 应用程序、框架和 CLR 而言,概念和危险是相同的。
I would say feature checking with (graceful) fallback is thus a much more reliable and robust way to make sure your application is downwards compatible. If you only want your application to work with a specific version of .NET or newer, don't do anything special at all and count on the backwards compatibility of .NET itself.
因此,我会说使用(优雅的)回退进行功能检查是确保您的应用程序向下兼容的更可靠和健壮的方法。如果您只希望您的应用程序使用特定版本的 .NET 或更新版本,请不要做任何特别的事情,并依靠 .NET 本身的向后兼容性。
回答by James L
To determine exactly which patch of .NET your application is running, make this call to find the build number of mscorlib:
要准确确定您的应用程序正在运行的 .NET 补丁,请进行此调用以查找 mscorlib 的内部版本号:
System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).ProductVersion
It currently returns 4.6.1055.0 for me, which corresponds to .NET 4.6.1.
它目前为我返回 4.6.1055.0,对应于 .NET 4.6.1。
回答by Govert
The .NET Framework 4.5 is an in-place upgrade to 4.0. This loosely means that if you are running on a version 4.0 or 4.5 runtime, and 4.5 is installed, then you are certainly running on 4.5. Your check might go as follows.
.NET Framework 4.5 是到 4.0 的就地升级。这大致意味着,如果您在 4.0 或 4.5 版运行时上运行,并且安装了 4.5,那么您肯定在 4.5 上运行。您的支票可能如下所示。
Let's call both the 4.0 and 4.5 runtimes 4.0-versioned runtimes.
让我们将 4.0 和 4.5 运行时都称为4.0-versioned runtimes。
Check whether you are running on a 4.0-versioned runtime:
- If your assembly is compiled to target .NET 4.0, or
Environment.Version.Major == 4 && Environment.Version.Minor == 0
then you are running on a 4.0-versioned runtime.
Check whether your 4.0-versioned runtime is actually version 4.0 or 4.5, by checking the installed version:
- Under the key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client, check the_Version_value. If it starts with"4.0"you are running on the 4.0 runtime, if it starts with"4.5"you are running on the 4.5 runtime.
- Under the key
检查您是否在 4.0 版本的运行时上运行:
- 如果您的程序集编译为面向 .NET 4.0,或者
Environment.Version.Major == 4 && Environment.Version.Minor == 0
那么您正在运行 4.0 版本的运行时。
通过检查已安装的版本,检查您的 4.0 版本运行时实际上是 4.0 版还是 4.5 版:
- 在键下
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client,检查_Version_值。如果它开始于"4.0"您在 4.0 运行时上运行,如果它开始于"4.5"您在 4.5 运行时上运行。
- 在键下
回答by Glenn Slayden
James provides the great answerof fetching the "product version" of mscorlib.dllat runtime:
James 提供了在运行时获取mscorlib.dll的“产品版本”的很好的答案:
(using System.Diagnostics;)
( using System.Diagnostics;)
FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).ProductVersion
This works well, but you can get a more fine-grained result by examining System.dllinstead:
这很有效,但您可以通过检查System.dll来获得更细粒度的结果:
FileVersionInfo.GetVersionInfo(typeof(Uri).Assembly.Location).ProductVersion
Notice the slight difference is to use the assembly for typeof(Uri)instead of typeof(int), since the former is defined in System.dllas opposed to mscorlib.dllfor the latter. For a simple C# console program targeting .NET 4.7.1, the difference, as currently reported on my system, is as follows:
请注意,稍微不同的是使用程序集 fortypeof(Uri)而不是typeof(int),因为前者的定义与后者System.dll相反mscorlib.dll。对于面向.NET 4.7.1的简单 C# 控制台程序,目前在我的系统上报告的差异如下:
...\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll 4.7.2600.0 ...\Microsoft.NET\Framework\v4.0.30319\System.dll 4.7.2556.0
...\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll 4.7。2600.0 ...\Microsoft.NET\Framework\v4.0.30319\System.dll 4.7。2556.0
As for whether the distinction is useful or how to use the more detailed information, that will depend on the particular situation.
至于区分是否有用,或者如何使用更详细的信息,这将取决于具体情况。
回答by Vipul
You can test whether the .NET Framework 4.5or the .NET Framework 4is installed by checking the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Fullsubkey in the registry for a DWORDvalue named Release. The existence of this DWORDindicates that the .NET Framework 4.5has been installed on that computer. The value of Releaseis a version number. To determine if the final release version of the .NET Framework 4.5is installed, check for a value that is equal to or greater than 378389.
您可以通过检查注册表中的子项中名为的值来测试安装的是.NET Framework 4.5还是.NET Framework 4。这表明该计算机上已安装.NET Framework 4.5。的值是版本号。要确定是否安装了.NET Framework 4.5的最终发行版,请检查是否存在等于或大于 的值。HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\FullDWORDReleaseDWORDRelease378389
回答by Matteo Mosca
From what I understood from online resources, .NET 4.5 will act in a similar way (although not completely the same) as 3.5 did for 2.0: your version number remains the same (if you run 3.5 code the CLR version will be 2.0), and it will act as an update of the existing CLR 4.0.
根据我从在线资源中了解到的情况,.NET 4.5 的行为方式与 3.5 对 2.0 的行为类似(尽管不完全相同):您的版本号保持不变(如果您运行 3.5 代码,则 CLR 版本将为 2.0),它将作为现有 CLR 4.0 的更新。
So you'll be able to have coexisting 2.0 upgraded to 3.5 and 4.0 upgraded to 4.5.
因此,您将能够将共存的 2.0 升级到 3.5,并将 4.0 升级到 4.5。
What remains unclear is if it will be possible to make (or work on existing) projects in 4.0 without having to upgrade them to 4.5: currently you can make 2.0 or 3.5 projects in Visual Studio 2010, but maybe it won't be the same for 4.0 and 4.5.
尚不清楚的是,是否可以在 4.0 中制作(或处理现有)项目而不必将它们升级到 4.5:目前您可以在 Visual Studio 2010 中制作 2.0 或 3.5 项目,但可能不会相同对于 4.0 和 4.5。
回答by picolino
Starting with .NET Core 3.0 (and .NET Standard 2.1) situation is changed and now Environment.Versionworking properly. So you can use this property to detect dot net version you using.
从 .NET Core 3.0(和 .NET Standard 2.1)开始,情况有所改变,现在可以Environment.Version正常工作。因此,您可以使用此属性来检测您使用的 dot net 版本。
System.Console.WriteLine($"Environment.Version: {System.Environment.Version}");
// Old result
// Environment.Version: 4.0.30319.42000
//
// New result
// Environment.Version: 3.0.0
See documentationfor further information.
有关更多信息,请参阅文档。

