Q:

Copy files to output directory using csproj dotnetcore

So my issue is pretty simple. I have some files that I want to be copied to the build output directory whether it is a debug build or a release publish. All of the information I can find is about the old json config approach. Anyone have an example using the csproj with dotnetcore?

A:

There's quite a few ways to achieve your goals, depending on what your needs are.
The easiest approach is setting the metadata (CopyToOutputDirectory / CopyToPublishDirectory) items conditionally (assuming .txt being a None item instead of Content, if it doesn't work, try <Content> instead):
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <None Update="foo.txt" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

If more control is required, the most versatile approach is to add custom targets that hooks into the build process in the csproj file:
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
  <Copy SourceFiles="foo.txt" DestinationFolder="$(OutDir)" />
</Target>
<Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
  <Copy SourceFiles="foo.txt" DestinationFolder="$(PublishDir)" />
</Target>

This copies a file to the respective directories. For more options for the <Copy> task see its documentation. To limit this to certain configurations, you can use a Condition attribute:
<Target … Condition=" '$(Configuration)' == 'Release' ">

This Condition attribute can be applied both on the <Target> element or on task elements like <Copy>.

A:

While this helped me get my issue sorted, it didn't work for all files in a sub-directory. I also used Content Include rather than Content Update;
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="layouts\*.*">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>  
  </ItemGroup>

</Project>

A:

Place this in your .csproj file replacing nlog.config with the desired filepath.  Then simply save it and build your project:
<ItemGroup>
  <Content Update="Nlog.config">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content>
</ItemGroup>

