简体   繁体   English

C# - MSBuild 参考 - 复制到 CopyToOutputDirectory 的所有项目<itemgroup></itemgroup>

[英]C# - MSBuild reference - Copy to CopyToOutputDirectory all items of <itemGroup>

I'm trying to copy all files in a project to an output directory.我正在尝试将项目中的所有文件复制到 output 目录。

Right now I've unloaded my process and in my "TestProject.csproj" I have these options for the item I'd like to copy:现在我已经卸载了我的进程,在我的“TestProject.csproj”中我有这些选项用于我想复制的项目:

  <ItemGroup>
    <Content Include="TEST\file1.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="TEST\file2.xml" >
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

What i want is to be able to copy all items in that to folder, included other that gonna be added later on (...file3.xml, file4.xml ).我想要的是能够将其中的所有项目复制到文件夹中,包括稍后将添加的其他项目(...file3.xml,file4.xml)。

I don't want to be forced to manually add to every file the "PreserveNewest" behavior, but I would like to use some sort of "post-compile option" to make it work.我不想被迫手动将“PreserveNewest”行为添加到每个文件中,但我想使用某种“后编译选项”来使其工作。

Any advice?有什么建议吗?

You can specify default item metadata with item definitions :您可以使用项目定义指定默认项目元数据:

<ItemDefinitionGroup>
  <Content>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemDefinitionGroup>

This would apply as a default to all Content items.这将默认应用于所有Content项。 You should be able to constrain this to only XML files by using a condition:您应该能够通过使用以下条件将其限制为仅 XML 文件:

<ItemDefinitionGroup>
  <Content>
    <CopyToOutputDirectory Condition="'%(Content.Extension)'=='.xml'">PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemDefinitionGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM