Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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> 
like image 450
Mark Avatar asked Feb 19 '19 09:02

Mark


People also ask

How do I put files in a folder in .NET core?

First, the File is read as Binary Data into a Byte Array object using the ReadAllBytes method of the File class. And then the Byte Array object is sent for download using the File function. //Fetch all files in the Folder (Directory). string[] filePaths = Directory.

How do I change the publish path in Visual Studio?

To specify a publishing location With a project selected in Solution Explorer, on the Project menu, click Properties. Click the Publish tab. In ClickOnce for . NET Core 3.1 and .


1 Answers

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.

like image 176
Bardr Avatar answered Oct 02 '22 12:10

Bardr