Visual Studio / MSBuild - 编译时根据编译条件动态引用项目及复制重命名编译输出文件

本文介绍了如何在Visual Studio中根据编译配置动态引用不同的资源项目,通过在.csproj文件中添加Conditional References实现。同时,讲解了利用MSBuild的AfterBuild目标来复制并重命名编译输出文件,以满足特定的项目需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需求1:主程序需要引用一个资源项目,根据编译配置 Visual Stuido中的Configuration Manager的不同,选择引用不同的资源项目

可在项目文件.csproj中添加如下内容:

  <!-- Conditional References: Add conditional references here -->

  <ItemGroup>
    <ProjectReference Include="..\..\TargetLanguages\TargetLanguageThai\TargetLanguageThai.csproj" Condition=" '$(ConfigurationName)' == 'Thai' ">
      <Project>{7A3F6F20-D083-4B7C-8B17-8091CDE95E20}</Project>
      <Name>TargetLanguageThai</Name>
    </ProjectReference>
  </ItemGroup>


  <ItemGroup>
    <ProjectReference Include="..\..\TargetLanguages\TargetLanguageGermany\TargetLanguageGermany.csproj" Condition=" '$(ConfigurationName)' == 'Germany' ">
      <Project>{0A9B26F6-87A0-4E51-8FDA-9CAE7C006649}</Project>
      <Name>TargetLanguageGermany</Name>
    </ProjectReference>
  </ItemGroup>


  <ItemGroup>
    <ProjectReference Include="..\..\TargetLanguages\TargetLanguagesSpanish\TargetLanguageSpanish.csproj" Condition=" '$(ConfigurationName)' == 'Spanish' ">
      <Project>{DA4BE376-36BD-4AC1-9CBB-C800C2D2D680}</Project>
      <Name>TargetLanguageSpanish</Name>
    </ProjectReference>
  </ItemGroup>
  

注意使用这种方式添加的项目引用,所有的项目引用都会出现在References目录下,但是在编译时,条件引用会起作用(注意下图的Output输出):


参考:http://stackoverflow.com/questions/16556813/visual-studio-project-how-to-include-a-project-reference-based-on-some-defineco

[更新 2013-12-23]

由于所有资源项目都被引用,在编译时会被全部编译,可以通过Configuration Manager进行设置,只编译需要的项目:



项目需求2: 使用项目属性中的Post-Build 事件无法重命名输出文件,可以使用如下MSBuild task 实现文件复制到另一个目录同时重命名文件:

在.csproj中添加如下内容

<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
       Other similar extension points exist, see Microsoft.Common.targets.  -->
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
    <Copy SourceFiles="$(OutDir)$(TargetName).xap" DestinationFiles="$(SolutionDir)APPBuild\$(ConfigurationName)\Store\$(TargetName)_$(ConfigurationName).xap" ContinueOnError="false" />
  </Target>

参考:http://ardalis.com/how-to-move-or-rename-a-file-using-msbuild

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值