Project-system: How to copy a folder to output directory?

Created on 31 Jan 2018  路  6Comments  路  Source: dotnet/project-system

<ItemGroup>
  <Folder Include="$(SolutionDir)config\" CopyToOutputDirectory="Always" />
</ItemGroup>

It didn't work, what's the correct way to copy a folder to output/publish dir?

Area-External DiscussioQuestion

Most helpful comment

MSBuild doesn't have a built-in concept for this. There are two usual approaches:

  • Copy all of the contents of the folder instead

    • This works great as long as the contents of the directory are static. If files are added to the directory during the build, you must use a target to expand the wildcard, or it will copy only the files that were present at the beginning of the build.

<ItemGroup>
  <None Include="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
  • Use a custom target with a task that is designed to copy folders. One example is the MSBuildExtensions Robocopy Task.

All 6 comments

Sounds like a question for the Microsoft/MSBuild repo. @andygerlicher or @rainersigwald any ideas?

MSBuild doesn't have a built-in concept for this. There are two usual approaches:

  • Copy all of the contents of the folder instead

    • This works great as long as the contents of the directory are static. If files are added to the directory during the build, you must use a target to expand the wildcard, or it will copy only the files that were present at the beginning of the build.

<ItemGroup>
  <None Include="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
  • Use a custom target with a task that is designed to copy folders. One example is the MSBuildExtensions Robocopy Task.

Thanks @rainersigwald !

Thank you for pointing the way & answering.

@rainersigwald I have folder "resources" in my solution folder. This folder contains many other folder and files. I want only this content to be in my output folder (not the folder "resources" itself). I used your solution and add this lines to project file:

<ItemGroup>
    <None Include="$(SolutionDir)resources\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

But I get folder "resources" with all its content in my output folder. So all paths to files in app is broken. How to copy only content of specific folder to output folder?
(VS Community 2017 v 15.7.3)

Was this page helpful?
0 / 5 - 0 ratings