Visual Studio: How to “Copy to Output Directory” without copying the folder structure?

instead of <Content> use <ContentWithTargetPath> and specify target path, like this:

<ItemGroup>
  <ContentWithTargetPath Include="lib\some_file.dat">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <TargetPath>some_file.dat</TargetPath>
  </ContentWithTargetPath>
  <None Include="lib\some_file.dat" />
</ItemGroup>

Note that this entry may not be visible from Visual Studio (2012, 2015, 2017), but once manually added to the csproj, it will appear in Visual Studio. The target path will not be editable through the UI though.

Adding a <None> entry for the file will ensure that it still shows up in Visual Studio’s UI.

Leave a Comment