问题是我需要知道它是否是版本 3.5 SP 1。Environment.Version()
仅返回 2.0.50727.3053
。
我找到了这个解决方案,但我认为这需要时间比其价值多得多,所以我正在寻找一种更简单的方法。 是否可以?
The problem is that I need to know if it's version 3.5 SP 1. Environment.Version()
only returns 2.0.50727.3053
.
I found this solution, but I think it will take much more time than it's worth, so I'm looking for a simpler one. Is it possible?
发布评论
评论(24)
试试这个:
Try this one:
像这样的事情应该可以做到。 只需从注册表中获取值
对于 .NET 1-4:
Framework
是安装的最高版本,SP
是该版本的服务包。对于 .NET 4.5+(来自官方文档) :
Something like this should do it. Just grab the value from the registry
For .NET 1-4:
Framework
is the highest installed version,SP
is the service pack for that version.For .NET 4.5+ (from official documentation):
不知道为什么没有人建议遵循微软的官方建议此处。
这是他们推荐的代码。 当然它很丑陋,但它有效。
对于 .NET 1-4
对于 .NET 4.5 及更高版本
Not sure why nobody suggested following the official advice from Microsoft right here.
This is the code they recommend. Sure it's ugly, but it works.
For .NET 1-4
For .NET 4.5 and later
另一种不需要访问注册表的权限的方法是检查特定框架更新中引入的类是否存在。
An alternative method where no right to access the registry are needed, is to check for the existence of classes that are introduced in specific framework updates.
我遇到过这样的情况:我的 .NET 4.0 类库可能会从 .NET 4.0 或更高版本的程序集调用。
特定方法调用仅在从 4.5+ 程序集执行时才有效。
为了决定是否应该调用该方法,我需要确定当前执行的框架版本,这是一个非常好的解决方案,可以
更新
从 4.0 程序集
它var featureEnabled = GetCurrentFrameworkName().Version >= new Version (4, 5);
I had a situation where my .NET 4.0 class library might be called from an .NET 4.0 or higher version assemblies.
A specific method call would only work if executed from an 4.5+ assembly.
In Order to decide if I should call the method or not I needed to determine the current executing framework version and this is a pretty good solution for that
Update
this works from a 4.0 assembly
var featureEnabled = GetCurrentFrameworkName().Version >= new Version(4, 5);
Environment.Version()
为另一个问题给出了正确答案。 .NET 2.0、3 和 3.5 中使用相同版本的 CLR。 我想您可以在 GAC 中检查每个后续版本中添加的库。Environment.Version()
is giving the correct answer for a different question. The same version of the CLR is used in .NET 2.0, 3, and 3.5. I suppose you could check the GAC for libraries that were added in each of those subsequent releases.这曾经很简单,但 Microsoft 决定做出重大更改:在版本 4.5 之前,每个版本的 .NET 都驻留在
C:\Windows\Microsoft.NET\Framework 下的自己的目录中
(子目录v1.0.3705、v1.1.4322、v2.0.50727、v3.0、v3.5
和v4.0.30319
)。自版本 4.5 起,这一点已更改:.NET 的每个版本(即 4.5.x、4.6.x、4.7.x、4.8.x...)都安装在相同的子目录
v4.0.30319
- 因此您无法再通过查看Microsoft.NET\Framework
来检查已安装的 .NET 版本。要检查 .NET 版本,Microsoft 提供了两个不同的示例脚本,具体取决于正在检查的 .NET 版本,但我不喜欢为此使用两个不同的 C# 脚本。
所以我尝试将它们合并为一个,这是我创建的脚本
GetDotNetVersion.cs
(并针对 4.7.1 框架更新了它):在我的机器上,它输出:
随着时间的推移,唯一需要维护的是内部版本号,一旦 .NET 版本大于 4.7.1 出现,这可以通过修改函数
CheckFor45PlusVersion
轻松完成,您需要知道新版本的发布密钥然后您可以添加它。 例如:此发行密钥仍然是最新的密钥,并且对 Windows 10 的 Fall Creators 更新有效。如果您仍在运行其他(较旧的)Windows 版本,则还有另一个密钥 >这个来自微软的文档:
因此,如果您也需要它,则必须将
CheckFor45PlusVersion
添加到函数顶部。同样,它适用于较新的版本。 例如,我最近添加了4.8的检查。 您通常可以在 Microsoft 找到这些内部版本号。
注意:您不需要安装 Visual Studio,甚至不需要安装 PowerShell - 您可以使用
csc.exe
编译并运行上面的脚本,该脚本我已在此处进行了描述。更新:问题是关于.NET Framework,为了完整起见,我还想提一下如何查询 .NET Core 的版本 - 与上面的相比,这很简单:打开命令 shell 并输入:
,它将列出 .NET Core 版本号、Windows 版本以及每个相关运行时 DLL 的版本。
示例输出:
注意
如果您只需要版本号而不需要所有附加信息,则可以使用
dotnet --version
。在 64 位 Windows PC 上,可以将 x86 版本与 x64 版本并排安装 .NET Core 版本。 如果是这种情况,您将只能获得环境变量
PATH
中第一个的版本。如果您需要保持最新并希望了解每个版本。 要查询两个版本,请使用:在上面的示例中,两者是相同的,但如果您忘记更新两个实例,则可能会得到不同的结果! 请注意,
Program Files
适用于64 位 版本,Program Files (x86)
适用于32 位 版本。更新:如果您更喜欢将内部版本号保留在列表中,而不是保留在一系列 if 语句中(如 Microsoft 建议的那样),那么您可以将此代码用于
CheckFor45PlusVersion
反而:It used to be easy, but Microsoft decided to make a breaking change: Before version 4.5, each version of .NET resided in its own directory below
C:\Windows\Microsoft.NET\Framework
(subdirectoriesv1.0.3705, v1.1.4322, v2.0.50727, v3.0, v3.5
andv4.0.30319
).Since version 4.5 this has been changed: Each version of .NET (i.e. 4.5.x, 4.6.x, 4.7.x, 4.8.x, ...) is being installed in the same subdirectory
v4.0.30319
- so you are no longer able to check the installed .NET version by looking intoMicrosoft.NET\Framework
.To check the .NET version, Microsoft has provided two different sample scripts depending on the .NET version that is being checked, but I don't like having two different C# scripts for this.
So I tried to combine them into one, here's the script
GetDotNetVersion.cs
I created (and updated it for 4.7.1 framework):On my machine it outputs:
The only thing that needs to be maintained over time is the build number once a .NET version greater than 4.7.1 comes out - that can be done easily by modifying the function
CheckFor45PlusVersion
, you need to know the release key for the new version then you can add it. For example:This release key is still the latest one and valid for the Fall Creators update of Windows 10. If you're still running other (older) Windows versions, there is another one as per this documentation from Microsoft:
So, if you need that as well, you'll have to add
to the top of the function
CheckFor45PlusVersion
.Likewise it works for newer versions. For example, I have added the check for 4.8 recently. You can find those build numbers usually at Microsoft.
Note: You don't need Visual Studio to be installed, not even PowerShell - you can use
csc.exe
to compile and run the script above, which I have described here.Update: The question is about the .NET Framework, for the sake of completeness I'd like to mention how to query the version of .NET Core as well - compared with the above, that is easy: Open a command shell and type:
and it will list the .NET Core version number, the Windows version and the versions of each related runtime DLL as well.
Sample output:
Note that
if you only need the version number without all the additional information, you can use
dotnet --version
.on a 64 bit Windows PC, it is possible to install the x86 version side by side with the x64 version of .NET Core. If that is the case, you will only get the version that comes first in the environment variable
PATH
. That is especially important if you need to keep it up to date and want to know each version. To query both versions, use:In the example above, both are the same, but if you forgot to update both instances, you might get different results! Note that
Program Files
is for the 64bit version, andProgram Files (x86)
is the 32bit version.Update: If you prefer to keep the build numbers in a list rather than in a cascade of if-statements (as Microsoft suggested), then you can use this code for
CheckFor45PlusVersion
instead:据我所知,框架中没有内置方法可以让您执行此操作。 您可以检查此 帖子 获取有关通过读取 Windows 注册表值确定框架版本的建议。
AFAIK there's no built in method in the framework that will allow you to do this. You could check this post for a suggestion on determining framework version by reading windows registry values.
此类允许您的应用程序抛出一条优雅的通知消息,而不是在找不到正确的 .NET 版本时崩溃。 您需要做的就是在主代码中执行以下操作:
默认情况下需要 4.5.2,但您可以根据自己的喜好进行调整,类(随意用 Console 替换 MessageBox):
已更新为 4.8:
当他们稍后添加新版本时可以轻松扩展。 在 4.5 之前我没有关心任何事情,但你明白了。
This class allows your application to throw out a graceful notification message rather than crash and burn if it couldn't find the proper .NET version. All you need to do is this in your main code:
By default it takes 4.5.2, but you can tweak it to your liking, the class (feel free to replace MessageBox with Console):
Updated for 4.8:
Easily extendable when they add a new version later on. I didn't bother with anything before 4.5 but you get the idea.
我试图将所有答案合并成一个整体。
使用:
NetFrameworkUtilities.GetVersion()
将返回 .NET Framework 当前可用的版本,如果不存在,则返回 null。此示例适用于 .NET Framework 版本 3.5+。 它不需要管理员权限。
我想指出的是,您可以使用运算符 << 来检查版本。 和> 像这样:
代码:
I tried to combine all the answers into a single whole.
Using:
NetFrameworkUtilities.GetVersion()
will return the currently available Version of the .NET Framework at this time or null if it is not present.This example works in .NET Framework versions 3.5+. It does not require administrator rights.
I want to note that you can check the version using the operators < and > like this:
Code:
属性返回运行应用程序的 .NET 安装的名称:
它返回 .NET 5 控制台应用程序上的值“.NET 5.0.3”。
来自 Microsoft 的更多信息:
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription 属性
property returns the name of the .NET installation on which an app is running:
It returns the value ".NET 5.0.3" on a .NET 5 Console app.
More info from Microsoft:
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription Property
感谢您的这篇文章非常有用。
我必须稍微调整一下才能检查框架 2.0,因为注册表项无法直接转换为双精度。 这是代码:
Thank you for this post which was quite useful.
I had to tweak it a little in order to check for framework 2.0 because the registry key cannot be converted straightaway to a double. Here is the code:
使用类 DA.VersionNetFramework
结果:
当前操作系统信息:
机器名称:D1
平台:Win32NT
Microsoft Windows NT 6.2.9200.0
.NET Framework 环境信息:
环境.版本4.0.30319.42000
Windows 8.1 64 位或更高版本上的 .NET 4.6
来自注册表的 .NET Framework 信息:
v2.0.50727 2.0.50727.4927 SP2
v3.0 3.0.30729.4926 SP2
v3.5 3.5.30729.4926 SP1
v4
客户端4.6.00079
完整版 4.6.00079
v4.0
客户端 4.0.0.0
.NET Framework 4.5 或更高版本 来自注册表的信息:
版本:4.6随Windows 10或更高版本安装
更新历史记录
Microsoft .NET Framework 4 客户端配置文件
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 扩展
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 多目标包
(KB2504637) 的 KB2504637 更新
using class DA.VersionNetFramework
Result:
Current OS Information:
Machine Name: D1
Platform: Win32NT
Microsoft Windows NT 6.2.9200.0
.NET Framework Environment Information:
Environment.Version 4.0.30319.42000
.NET 4.6 on Windows 8.1 64 - bit or later
.NET Framework Information From Registry:
v2.0.50727 2.0.50727.4927 SP2
v3.0 3.0.30729.4926 SP2
v3.5 3.5.30729.4926 SP1
v4
Client 4.6.00079
Full 4.6.00079
v4.0
Client 4.0.0.0
.NET Framework 4.5 or later Information From Registry:
Version: 4.6 installed with Windows 10 or later
Update History
Microsoft .NET Framework 4 Client Profile
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 Extended
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 Multi-Targeting Pack
KB2504637 Update for (KB2504637)
我更改了 Matt 的类,以便可以在任何项目中重用它,而无需在控制台上打印所有检查并返回安装了正确的最大版本的简单字符串。
I have changed Matt's class so it can be reused in any project without printing all its checks on Console and returning a simple string with correct max version installed.
您可以获得有用的字符串,而无需接触注册表或引用可能已加载或未加载的程序集。 mscorlib.dll 和其他系统程序集定义了 AssemblyFileVersionAttribute,并且基于 Visual Studio 提供的参考程序集,它对于每个版本的 .NET 似乎都是唯一的。
4.5 版本有点偏离,因为该版本中标记为 4.0,但 4.6 以后的版本似乎至少有次要版本匹配。 这样做似乎比依赖某些安装程序注册表项并必须与一组固定值进行比较更能适应未来的情况。
我在这里找到了参考程序集:
我在哪里获得了以下版本:
You can get a useful string without having to touch the registry or reference assemblies which may or may not be loaded. mscorlib.dll and other System assemblies have AssemblyFileVersionAttribute defined, and it seems to be unique for each version of .NET, based on the reference assemblies provided with Visual Studio.
Version 4.5 is a bit off, because it's marked 4.0 in that version, but 4.6 onward appear to have the minor version matching at least. Doing it this way seems like it would be more future proof than depending on some installer registry key and having to compare against a fixed set of values.
I found the reference assemblies here:
Where I got the following versions:
使用 .NET 4.6.2 进行更新。 检查与之前响应相同的注册表中的 Release 值:
Update with .NET 4.6.2. Check Release value in the same registry as in previous responses:
对于那些寻找所有 .net 相关事物版本的人。
我结合了.net core+framework相关信息。
示例输出,
For those looking for all .net-related things' versions.
I combined .net core+framework related info.
Example output,
有点大,但看起来它是最新的微软奇怪的东西:
Little large, but looks like it is up-to-date to Microsoft oddities:
从版本 4.5 开始,Microsoft 更改了在注册表中存储 .NET Framework 指示器的方式。 有关如何检索 .NET 框架和 CLR 版本的官方指南可以在此处找到:https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
我包含了其代码的修改版本以解决赏金问题关于如何确定 4.5 及更高版本的 .NET 框架的问题:
As of version 4.5 Microsoft changed the way it stores the .NET Framework indicator in the registry. There official guidance on how to retrieve the .NET framework and the CLR versions can be found here: https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
I am including modified version of their code to address the bounty question of how you determine the .NET framework for 4.5 and higher here:
这是我找到的解决方案:
This is the solution I landed on:
如果您的计算机已连接到互联网,请转至 smallestdotnet,下载并执行 .NET 检查器可能是最简单的方法。
如果您需要确定版本的实际方法,请查看 github 上的源,尤其是。 Constants.cs 这将为您的 .net 4.5 提供帮助然后,修订部分是相关部分:
If your machine is connected to the internet, going to smallestdotnet, downloading and executing the .NET Checker is probably the easiest way.
If you need the actual method to deterine the version look at its source on github, esp. the Constants.cs which will help you for .net 4.5 and later, where the Revision part is the relvant one:
我发现使用
Version
类更容易:(代码是 C#8)
I find it easier to work with the
Version
class:(code is C#8)