58

I have the following folder structure for my .NET Core 2.1 project:

How can I include folder AppData and all of its subfolders and files when I publish the solution?

I tried adding this to .csproj file but it didn't work:

<ItemGroup>
    <Folder Include="AppData\*" />
</ItemGroup>

EDIT

I also tried with this and it didn't work:

<ItemGroup>
    <Content Include="AppData\**" LinkBase="AppData" />
</ItemGroup>
Mark
  • 1,374
  • 1
  • 11
  • 34
  • Possible duplicate of [dotnet core publish: include/exclude dir in output](https://stackoverflow.com/questions/43569821/dotnet-core-publish-include-exclude-dir-in-output) – Iskander Raimbaev Feb 19 '19 at 09:27
  • I tried with `` and it didn't work. – Mark Feb 19 '19 at 09:29
  • 3
    Try: ` PreserveNewest ` – Bardr Feb 19 '19 at 09:36
  • 1
    @Bardr It works, but only for folders which contain files/subfolders. How can I copy empty folders as well? – Mark Feb 19 '19 at 09:41
  • I don't know if this is possible but there is workaround: ` ` This will create AppData folder after publishing if it doesn't exist. It won't exist only if it's empty while publishing. – Bardr Feb 19 '19 at 09:45

5 Answers5

100

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.

Bardr
  • 2,089
  • 2
  • 11
  • 21
  • 1
    This is the implementation I prefer since it doesn't rely on creating a placeholder file. With the placeholder file approach, somewhere down the road, someone may wonder why the file is there and ask what it does. – SRQ Coder Feb 20 '19 at 04:41
  • @Bardr and after you include this in the build ( when oublish ) how we are going to retrieve the files inside the folder? I`ve tried many ways but i keep getting errors that the folder cannot be found... In the build action, i set the files to contents – Csibi Norbert Aug 09 '20 at 08:59
21

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

Dmitriy Ivanov
  • 740
  • 7
  • 10
  • 3
    You can also add LinkBase="MyDirectory" to the element if you want to specify a folder to place the files into within the publish directory. – Andrew Nov 09 '20 at 07:36
5

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.

G. B.
  • 458
  • 2
  • 13
4

None of the above solutions worked for me. So, I took the same approach taken in "React project template" and I added this code to my .csproj file:

  <Target Name="PublishFrontend" AfterTargets="ComputeFilesToPublish">
    <ItemGroup>
      <DistFiles Include="ClientApp\build\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
manna
  • 138
  • 2
  • 7
4

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>