未能加载文件或程序集或它的某一个依赖项(针对资源问题,x86文件在x64上编译失败)

来源:互联网 发布:php function_exists 编辑:程序博客网 时间:2024/06/09 18:06


在开发中遇到过这样的问题,在x86系统上面可以编译通过的程序,在x64系统上编译出错,提示 未能加载文件或程序集 或它的某一个依赖项,如下图所示:

产生此问题的原因:

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 官方网址为:

http://support.microsoft.com/kb/2028833