如何找出可执行文件需要运行哪个版本的 .NET Framework?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/325918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:42:08  来源:igfitidea点击:

How to find out which version of the .NET Framework an executable needs to run?

.netversionexecutable

提问by Sam

I've got an executable file, and I would like to know which versions of the .NET framework this file needs to be started.

我有一个可执行文件,我想知道该文件需要启动哪个版本的 .NET 框架。

Is there an easy way to find this information somewhere?

有没有一种简单的方法可以在某处找到这些信息?

(So far I tried ILDASMand DUMPBINwithout any luck.)

(到目前为止,我尝试了ILDASMDUMPBIN,但没有任何运气。)

采纳答案by Scott Dorman

I think the closest you can reliably get is to determine what version of the CLRis required. You can do this by using ILDASM and looking at the "MANIFEST" node or Reflector and looking at the dissasembly view of the "Application.exe" node as IL. In both cases there is a comment that indicates the CLR version. In ILDASM, the comment is "// Metadata version" and in Reflector the comment is "Target Runtime Version".

我认为您可以可靠地获得的最接近的是确定需要哪个版本的CLR。您可以通过使用 ILDASM 并查看“MANIFEST”节点或反射器并将“Application.exe”节点的分解视图作为 IL 来执行此操作。在这两种情况下,都有指示 CLR 版本的注释。在 ILDASM 中,注释是“// Metadata version”,在 Reflector 中,注释是“Target Runtime Version”。

Here are examples for a .NET WinForms application named WindowsFormsApplication1.exe:

以下是名为 WindowsFormsApplication1.exe 的 .NET WinForms 应用程序的示例:

ILDASM:

伊尔达斯姆:

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
.assembly extern System
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}

Reflector:

反射器:

.module WindowsFormsApplication1.exe
.subsystem 0x0002
// MVID: {CA3D2090-16C5-4899-953E-4736D6BC0FA8}
// Target Runtime Version: v2.0.50727

You can also look at the list of referenced assemblies and look for the reference with the highest version number.

您还可以查看引用的程序集列表并查找具有最高版本号的引用。

Again, using ILDASM looking at the "MANIFEST" node data:

再次使用 ILDASM 查看“MANIFEST”节点数据:

.assembly extern System.Drawing
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
  .ver 2:0:0:0
}
.assembly extern System.Core
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 3:5:0:0
}

And using Reflector, looking at the dissambly (still as IL) for each reference listed:

并使用 Reflector,查看列出的每个参考的可拆卸性(仍为 IL):

.assembly extern System.Core
{
    .ver 3:5:0:0
    .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
}

By finding the reference with the highest version metadata you can determine what version of the Framework that reference came from, which would indicate that you need the same version of the Framework installed for the application to run. That being said, I wouldn't treat this as 100% reliable, but I don't think it will change any time soon.

通过查找具有最高版本元数据的引用,您可以确定引用来自哪个版本的 Framework,这表明您需要安装相同版本的 Framework 才能运行应用程序。话虽如此,我不会将其视为 100% 可靠,但我认为它不会很快改变。

回答by Asain Kujovic

Using Notepad, three decades old, 200kb in size, preinstalled tool:

使用Notepad,三年前,大小为 200kb,预装工具:

  • open application with notepad appname.exe,
  • search for word "framework",
  • repeat last search with F3until .NET Framework,version=vX.Yshows up
  • if nothing found (versions below 3.0) search for v2.... still 100 times easier then installing gigabytes of dot net analyzer tools and garbage studios.
  • 打开应用程序notepad appname.exe
  • 搜索词“框架”,
  • 重复上次搜索F3直到.NET Framework,version=vX.Y出现
  • 如果没有找到(低于 3.0 的版本)搜索v2.... 仍然比安装千兆字节的 dot net 分析器工具和垃圾工作室容易 100 倍。

Any other editor/viewer can open binaries too, like Notepad++ or totalCommander's great text/hex viewer lister.

任何其他编辑器/查看器也可以打开二进制文件,例如 Notepad++ 或 totalCommander 的出色文本/十六进制查看器列表器

回答by Daniel A. White

A more simplified approach would be to use dotPeekand see what shows up in the tree.

更简化的方法是使用dotPeek并查看树中显示的内容。

See the properties panel: enter image description here

查看属性面板: 在此处输入图片说明

回答by tsandhol

You can now use ILSpyto examine the target framework of an assembly. After loading the assembly, click on the root of the assembly node, and you can find the information under the TargetFramework declaration:

您现在可以使用ILSpy检查程序集的目标框架。加载程序集后,点击程序集节点的根节点,可以在TargetFramework声明下找到信息:

[assembly: TargetFramework(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]

回答by Andrew Hare

From code you can use Assembly.ImageRuntimeVersionbut by looking at the file probably the best thing to do would be to use reflector and see which version of mscorlibis being referenced.

从您可以使用的代码中,Assembly.ImageRuntimeVersion但通过查看文件,最好的办法可能是使用反射器并查看mscorlib正在引用的版本。

Edit:Even better would be to use ildasm, open your assembly and then view the manifest for the assembly. The first line of the manifest will tell you the exactversion of CLR that the assembly was built for.

编辑:更好的是使用ildasm,打开你的程序集,然后查看程序集的清单。清单的第一行将告诉您程序集所针对的 CLR的确切版本。

回答by Vince Fedorchak

You can use a tool called CorFlags.exe. It has been around since .NET 2.0, and I know for sure that it is included in the Windows SDK 7.0. By default (on Windows XP Pro) it is installed to C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\CorFlags.exe. Provide it with the file path to a managed module (without any other command-line flags) to display its header information, which includes the version.

您可以使用名为 CorFlags.exe 的工具。它从 .NET 2.0 开始就存在,我确信它包含在 Windows SDK 7.0 中。默认情况下(在 Windows XP Pro 上)它安装到 C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\CorFlags.exe。为其提供托管模块的文件路径(没有任何其他命令行标志)以显示其头信息,其中包括版本。

Keep in mind that this utility is designed to modify the PE32 header of a module, so don't use any of the flags until you read the documentationcarefully.

请记住,此实用程序旨在修改模块的 PE32 标头,因此在仔细阅读文档之前不要使用任何标志。

回答by Sean B

From the command line: find "Framework" MyApp.exe

从命令行: find "Framework" MyApp.exe

回答by Denis

Or you can just find out which reference of System.Core it has. That will tell you the .NET Framework version this app is using. For 2.0 the version of System.Core will be 2.0.xxx.xxx. For 3.5 the version will be 3.5.xxx.xxx, etc.

或者您可以找出它具有 System.Core 的哪个引用。这将告诉您此应用程序正在使用的 .NET Framework 版本。对于 2.0,System.Core 的版本将为 2.0.xxx.xxx。对于 3.5,版本将为 3.5.xxx.xxx 等。

回答by Pierz

On Linux/OSX/unix you can use:

在 Linux/OSX/unix 上,您可以使用:

strings that_app.exe | grep 'v2.\|Framework'

回答by Mark Walker

You can obtain the .NET version of a file in Windows using Powershell. The following script;

您可以使用 Powershell 在 Windows 中获取文件的 .NET 版本。以下脚本;

$path='.\'
$ErrorActionPreference = "SilentlyContinue"
$files=Get-ChildItem -Path $path -Recurse -include *.dll,*.exe
foreach($file in $files)
{
    $filename = $file.BaseName
    $version = $([System.Reflection.Assembly]::ReflectionOnlyLoadFrom($file.FullName).GetCustomAttributesData() |
                 select-object -ExpandProperty ConstructorArguments | 
                 select-object -ExpandProperty Value | 
                 select-string -Pattern '.NET')
    Write-Output "$filename,$version"
}

provides the following result; enter image description here

提供以下结果; 在此处输入图片说明

Note that the result extracted the .NET version for the exe files in that folder, but it will also do the same for a dll.

请注意,结果为该文件夹中的 exe 文件提取了 .NET 版本,但它也会为 dll 执行相同的操作。