.NET Core include folder in publish

后端 未结 4 868
时光说笑
时光说笑 2020-12-14 00:44

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

相关标签:
4条回答
  • 2020-12-14 00:58

    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

    0 讨论(0)
  • 2020-12-14 01:12

    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>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-14 01:23

    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.

    0 讨论(0)
提交回复
热议问题