大多数项目都需要编译32位和64位两种版本 VS中在配置管理器中可以直接选择X86以及X64两种模式分别生成对应版本的项目文件 但是项目引用的dll库有时候会有x64和x86两种格式,需要在生成两种版本时候加载不同的dll文件。 这里我遇到的情况是加载System.Data.SQLite.dll的数据库文件,会有区分x86和x64两种。之前切换生成64位和32位版的时候,都要删除引用并重新添加引用,非常麻烦。这里介绍一种方法可以设置一次,不需要每次都修改引用dll的路径。 首先找到解决方案中加载System.Data.SQLite.dll库的工程。找到对应的 工程名.csproj。使用文本编辑器打开 这个文件 ,这是一个使用xml方式的工程文件 。找到加载库的地方 如: [html]​view plain​​​ ​​copy​


  1. <Reference Include="System.Data.SQLite">
  2. <SpecificVersion>False</SpecificVersion>
  3. <HintPath>lib\SQLite-64\System.Data.SQLite.dll</HintPath>
  4. </Reference>

添加Condition选项 如: [html]​view plain​​​ ​​copy​


  1. <Reference Condition=" '$(Platform)' == 'AnyCPU'" Include="System.Data.SQLite">
  2. <SpecificVersion>False</SpecificVersion>
  3. <HintPath>lib\SQLite-86\System.Data.SQLite.dll</HintPath>
  4. </Reference>
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="System.Data.SQLite">
  6. <SpecificVersion>False</SpecificVersion>
  7. <HintPath>lib\SQLite-64\System.Data.SQLite.dll</HintPath>
  8. </Reference>

这样便可以实现切换x86和x64自动切换加载动态库路径 还有一种场景是这样的: 解决方案中一个工程生成dll,另一个工程加载此工程生成的dll 除了这个工程生成x86以及x64两个版本之外,还会有debug版和release版两种情况。这里的解决方案就比较简单了 原先是这样的 [html]​view plain​​​ ​​copy​


  1. <Reference Condition=" '$(Platform)' == 'x86'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
  2. <SpecificVersion>False</SpecificVersion>
  3. <HintPath>ConvertTaskManager\ConvertTaskManager\bin\Release\ConvertTasks.dll</HintPath>
  4. </Reference>
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
  6. <SpecificVersion>False</SpecificVersion>
  7. <HintPath>ConvertTaskManager\ConvertTaskManager\bin\x64\Release\ConvertTasks.dll</HintPath>
  8. </Reference>

只需要将路径中debug或release的目录替换成$(Configuration)即可 如下: [html]​view plain​​​ ​​copy​


  1. <Reference Condition=" '$(Platform)' == 'x86'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
  2. <SpecificVersion>False</SpecificVersion>
  3. <HintPath>ConvertTaskManager\ConvertTaskManager\bin\$(Configuration)\ConvertTasks.dll</HintPath>
  4. </Reference>
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
  6. <SpecificVersion>False</SpecificVersion>
  7. <HintPath>ConvertTaskManager\ConvertTaskManager\bin\x64\$(Configuration)\ConvertTasks.dll</HintPath>
  8. </Reference>