.NET Core include folder in publish

2020-02-25 08:25发布

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>

3条回答
等我变得足够好
2楼-- · 2020-02-25 08:54

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.

查看更多
干净又极端
3楼-- · 2020-02-25 08:57

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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-02-25 09:05

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

查看更多
登录 后发表回答