在开发中遇到过这样的问题,在x86系统上面可以编译通过的程序,在x64系统上编译出错,提示 未能加载文件或程序集 或它的某一个依赖项,如下图所示:
产生此问题的原因:
先检查确保应该添加的引用都已经添加, 若引用都正常,可能是由于系统环境发生变化引起的,.resx 文件,属于资源文件 ,由 XML 组成,可以加入任何资源,包括二进制等。
64 位系统上设置编译平台为 x86 的项目编译在特定的情况下比如当一个窗体上放有包含了图像的 ImageList 之后, ResGen 就会产生这种问题。当这个 ImageList 中没有图像时编译也是正常的,但是一旦编译就会引发这样的异常。
这 个错误产生的原因在于, VS2010 内部使用的编译器中,无论是 32 位还是 64 位的编译组件,都是纯 IL 的,也就是在 64 位系统中是以 64 位模式运行,这 与当前项目使用的平台设置无关。当编译的组件引用了一个标记为 x86 的库(仅 32 位模式)时,编译组件便会发生错误,无法加载,从而导致编译失败。
解决方案一:
1、在项目上右键, 卸载项目。
2、 编辑项目的 .csproj 文件,在根节点 </Project> 之前加入如下代码:
- <PropertyGroup>
- <ForceResGen32Bit Condition="'$(MSBuildToolsVersion)'=='4.0' And '$(PROCESSOR_ARCHITEW6432)'!='' And '$(TargetingClr2Framework)'=='true'
- And '$(PlatformTarget)'=='x86'">true</ForceResGen32Bit>
- </PropertyGroup>
- <Target Name="BeforeResGen" Condition="'$(ForceResGen32Bit)' == 'true'">
- <PropertyGroup>
- <ResGenSdkToolsPath>$(IntermediateOutputPath)ResGenForced32Bit\</ResGenSdkToolsPath>
- </PropertyGroup>
- <!-- Copy resgen.exe to intermediate working directory for UAC settings -->
- <Copy SourceFiles="$(TargetFrameworkSDKToolsDirectory)ResGen.exe" DestinationFiles="$(ResGenSdkToolsPath)ResGen.exe" />
- <!-- corflags.exe resgen.exe /32BIT+ /Force-->
- <Exec WorkingDirectory="$(ResGenSdkToolsPath)" Command=""$(TargetFrameworkSDKToolsDirectory)corflags.exe" ResGen.exe /32BIT+ /Force" />
- <!-- GenerateResource Task parameters
- Using the non-64bit Tracker.exe and indicate resgen.exe has been forced to x86 -->
- <PropertyGroup>
- <ResGenTrackerSdkPath>$(SDK40ToolsPath)</ResGenTrackerSdkPath>
- <ResGenToolArchitecture>Managed32Bit</ResGenToolArchitecture>
- <CacheTargetFrameworkSDKToolsDirectory>$(TargetFrameworkSDKToolsDirectory)</CacheTargetFrameworkSDKToolsDirectory>
- <TargetFrameworkSDKToolsDirectory>$(ResGenSdkToolsPath)</TargetFrameworkSDKToolsDirectory>
- </PropertyGroup>
- </Target>
- <Target Name="AfterResGen" Condition="'$(ForceResGen32Bit)' == 'true'">
- <PropertyGroup>
- <TargetFrameworkSDKToolsDirectory>$(CacheTargetFrameworkSDKToolsDirectory)</TargetFrameworkSDKToolsDirectory>
- </PropertyGroup>
- <RemoveDir Directories="$(ResGenSdkToolsPath)" Condition="Exists('$(ResGenSdkToolsPath)')" />
- </Target>
3、保存后,重新编译项目,编译通过。
解决方案二:
将此项目的编译目标框架设为 .Net Framewrok4.0 或者更高框架即可编译通过。
关于此问题的 MicroSoft 官方网址为: