C#工程中 使用targets和props实例

描述:

  需要使用的C++类库区分x64和x86版本,在C#工程编译时根据编译选项自动选择dll库版本并复制到输出路径

解决:

  1.准备dll库编译好的两个版本;

  2.在C#项目中引用随编一个(一般x64,x86引用文件都一致);  

  3.卸载该项目后,修改C#工程文件(*.csproj)

    3.1 找到自动生成的引用节点

<Reference Include="CefSharp">
<HintPath>DLLs\x64\CefSharp.dll</HintPath>
</Reference>
    3.2 添加引用条件   Condition=" '$(Platform)' == 'x64'"
<Reference Include="CefSharp" Condition=" '$(Platform)' == 'x64'">
<HintPath>DLLs\x64\CefSharp.dll</HintPath>
</Reference>
    3.3 同理X86版本进行修改
<Reference Include="CefSharp" Condition=" '$(Platform)' == 'x86'">
<HintPath>DLLs\x86\CefSharp.dll</HintPath>
</Reference>
  4.保存后重新加载该工程即可;
 

Note:

  1.貌似也可以动态指定路径:

<Reference Include="CefSharp.Core">
  <HintPath>DLLs\$(Platform)\CefSharp.Core.dll</HintPath>
</Reference>
  2.添加自动复制脚本
  2.1 添加targets文件,在该文件中定义所含目标文件(文件内容):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)\DLLs\x86')">
    <CefDlls32 Include="$(MSBuildThisFileDirectory)\DLLs\x86\*.*" />
  </ItemGroup>
  
  <ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)\DLLs\x64')">
    <CefDlls64 Include="$(MSBuildThisFileDirectory)\DLLs\x64\*.*" />
  </ItemGroup>
</Project>

  2.2 添加props文件,并在文件中写入复制信息以及路径:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

  <Target Name="CefSharpCopyDlls86" BeforeTargets="AfterBuild" Condition="'$(Platform)' == 'x86'">
    <Message Importance="high" Text="Copying cef.dlls x86 binaries" />
    <Copy SourceFiles="@(CefDlls32)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />  
    </Target>  

  <Target Name="CefSharpCopyDlls64" BeforeTargets="AfterBuild" Condition="'$(Platform)' == 'x64'">
    <Message Importance="high" Text="Copying cef.dlls x64 binaries" />
    <Copy SourceFiles="@(CefDlls64)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />
  </Target>  
  
</Project>

   2.3 在项目文件中导入两个文件,ok;

<Import Project="DLLs\CefDlls.Native.props" Condition="Exists('DLLs\CefDlls.Native.props')" />
<Import Project="Dlls\cef.dlls.targets" Condition="Exists('Dlls\cef.dlls.targets')" />
  
  2.4 验证成功,编译输出信息;

 

 

posted @ 2017-08-31 16:20  Zima  阅读(5968)  评论(0编辑  收藏  举报