简体   繁体   English

.NET Core 在发布中包含文件夹

[英].NET Core include folder in publish

I have the following folder structure for my .NET Core 2.1 project:我的 .NET Core 2.1 项目具有以下文件夹结构:

How can I include folder AppData and all of its subfolders and files when I publish the solution?发布解决方案时,如何包含文件夹AppData及其所有子文件夹和文件?

I tried adding this to .csproj file but it didn't work:我尝试将其添加到 .csproj 文件中,但没有奏效:

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

EDIT编辑

I also tried with this and it didn't work:我也试过这个,但没有用:

<ItemGroup>
    <Content Include="AppData\**" LinkBase="AppData" />
</ItemGroup>

Adding this:添加这个:

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

to your .csproj file will copy AppData folder if it's not empty.如果它不是空的,你的.csproj文件将复制AppData文件夹。 For empty AppData folder you can use this workaround:对于空的AppData文件夹,您可以使用以下解决方法:

<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.如果它尚未包含在输出中,这将在发布后创建AppData文件夹。 Meaning this will create AppData folder only if it's empty while publishing.这意味着只有在发布时它是空的,这才会创建AppData文件夹。

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您可以在此处找到更多技巧: https : //docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj

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.其他方式:添加后构建步骤命令,创建目录。

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:因此,我采用了在“React 项目模板”中采用的相同方法,并将此代码添加到我的 .csproj 文件中:

  <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>

First solution, if run dotnet build or dotnet publish will add the folder inside bin.第一个解决方案,如果运行 dotnet build 或 dotnet publish 将在 bin 中添加文件夹。

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

Second solution, if run dotnet publish will add the folder inside bin.第二种解决方案,如果运行 dotnet publish 将在 bin 中添加文件夹。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM