.net - 系统.BadImageFormatException : Could not load file or assembly (from installutil. exe)

标签 .net windows-services x86 64-bit x86-64

我尝试使用 InstallUtil.exe 安装 Windows 服务并收到错误消息

System.BadImageFormatException: Could not load file or assembly '{xxx.exe}' or one of its dependencies. An attempt was made to load a program with an incorrect format.

什么给出了?

<小时/>

编辑:(不是OP)从dup中提取的完整消息获得了更多点击[用于谷歌能力]:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe C:\xxx.exe Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1 Copyright (c) Microsoft Corporation. All rights reserved.

Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:///C:\xxx.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format..

最佳答案

一些更完整的细节,以防它对某人有帮助......

请注意,目前出现此异常的最常见原因是尝试将 32 位特定 (/platform:x86) DLL 加载到 64 位进程中,反之亦然(即将 64 位特定 (/platform:x64) DLL 加载到 32 位进程中。如果您的平台是非特定的(/platform:AnyCpu),则不会出现这种情况(假设没有引用的依赖项具有错误的位数)。

换句话说,运行:

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil.exe

或者:

%windir%\Microsoft.NET\Framework64\v2.0.50727\installutil.exe

将不起作用(替换为其他框架版本:v1.1.4322(仅限32位,因此不会出现此问题)和v4.0.30319(根据需要)如上所述)。

显然,正如另一个答案中所述,您还需要您正在运行的 installutil 的 .NET 版本号 >=(最好 =)您所运行的 EXE/DLL 文件的版本号。正在运行安装程序。

最后,请注意in Visual Studio 2010, the tooling will default to generating x86 binaries (rather than Any CPU as previously)。

Complete details of System.BadImageFormatException (说唯一的原因是咬合不匹配确实过于简单化了!)。

x64 安装程序下出现 BadImageFormatException 的另一个原因是 in Visual Studio 2010, the default .vdproj Install Project type generates a 32-bit InstallUtilLib shim, even on an x64 system (在页面上搜索“64 位托管自定义操作引发 System.BadImageFormatException 异常”)。

关于.net - 系统.BadImageFormatException : Could not load file or assembly (from installutil. exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/323140/

相关文章:

assembly - Assembly (MASM) 中的 Push 操作是否使寄存器的新值与推送的值相同?

c# - 如何使用负退出代码调用 GetExitCodeProcess?

.net - 机器的最大 TCP 连接数

C#:制作安装 WPF 应用程序 (ClickOnce) 和 Windows 服务的安装程序

jenkins - 如何禁用已安装的 jenkins 作为 Windows 服务并在 cmd 中运行相同的 jenkins?

assembly - X86:保护模式、GDT、IDT

delphi - 将 Delphi 风格的 ASM 翻译成英文?

c# - 从表中删除*

.net - 对 .NET Standard 与 .NET Core "interface vs implementation"的解释感到困惑

.net - 使用 MSBuild 命令行指定发布版本作为项目的程序集版本