1

I'm trying to copy all files in a project to an output directory.

Right now I've unloaded my process and in my "TestProject.csproj" I have these options for the item I'd like to copy:

  <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 ).

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.

Any advice?

Dilshod K
  • 2,924
  • 1
  • 13
  • 46
okkappa
  • 31
  • 4

1 Answers1

2

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. You should be able to constrain this to only XML files by using a condition:

<ItemDefinitionGroup>
  <Content>
    <CopyToOutputDirectory Condition="'%(Content.Extension)'=='.xml'">PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemDefinitionGroup>
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Worked. I've seen that for there's no attribute for a custom OutputDirectory. It's possbile to use the same approach to define a custom outputPath? – okkappa Sep 20 '19 at 16:57
  • I think the metadata for where to copy that item is `TargetPath`, which usually is determined during build by other targets, but it's possible that you can set it yourself. – Joey Sep 22 '19 at 12:09