.NET Core include folder in publish

You can put a placeholder file in it (or use your existing files). Then add the file to the project and set the file properties: Copy To Output Directory: Copy if newer or Copy always.

Other way: add a post build step command, that creates the directory.


Adding this:

<ItemGroup> 
  <Content Include="AppData\**"> 
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
  </Content> 
</ItemGroup>

to your .csproj file will copy AppData folder if it's not empty. For empty AppData folder you can use this workaround:

<Target Name="CreateAppDataFolder" AfterTargets="AfterPublish">
  <MakeDir Directories="$(PublishDir)AppData" Condition="!Exists('$(PublishDir)AppData')" /> 
</Target>

This will create AppData folder after publish if it won't be already included in output. Meaning this will create AppData folder only if it's empty while publishing.


There is simple and useful solution:

  <ItemGroup>
    <Content Include="AppData\**" CopyToPublishDirectory="PreserveNewest"/>
  </ItemGroup>

You can find more tricks here: https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj


First solution, if run dotnet build or dotnet publish will add the folder inside bin.

<ItemGroup>
  <None Update="AppData\**"  CopyToOutputDirectory="PreserveNewest"  />
</ItemGroup>

Second solution, if run dotnet publish will add the folder inside bin.

<ItemGroup>
  <Content Include="AppData\**" CopyToPublishDirectory="PreserveNewest"/>
</ItemGroup>