Aleks Vujic
Aleks Vujic

Reputation: 2261

.NET Core include folder in publish

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>

Upvotes: 79

Views: 110027

Answers (6)

Mohammadreza Askari
Mohammadreza Askari

Reputation: 583

I tried to put 'logs' directory into publish out directory, finally this lines into Project.csproj works (mind to set 'Delete existing files = false' in publish setting):

<Target Name="LogDirectoryOnPublish" BeforeTargets="Publish">
    <RemoveDir Condition="Exists('$(PublishUrl)')" Directories="$(PublishUrl)" />
    <MakeDir Directories="$(PublishUrl)\logs\" />
</Target>

Upvotes: 0

Jhonny Ramirez Zeballos
Jhonny Ramirez Zeballos

Reputation: 3186

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>

Upvotes: 9

manna
manna

Reputation: 198

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>

Upvotes: 6

Dmitriy Ivanov
Dmitriy Ivanov

Reputation: 1300

There is simple and useful solution:

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

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

Upvotes: 31

Bardr
Bardr

Reputation: 2559

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.

Upvotes: 142

G. B.
G. B.

Reputation: 628

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.

Upvotes: 4

Related Questions