Home > Software design >  .NET Core copy subfolders to output on build and publish
.NET Core copy subfolders to output on build and publish

Time:09-03

I have a Microsoft.NET.Sdk.Web project and in the solution folder I have a Lib folder with subfolders which in turn have files, for example:

Lib
  sv
    myfile.dll
  de
    myfile.dll

I added the following to the csproj:

<ItemGroup>
    <Content Include="Lib\sv\**">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>
<ItemGroup>
    <Content Include="Lib\de\**">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

But the above is placing the subfolders and their files within the Lib folder in the output folder.

I need to have the following:

(OutputPath)
  sv
    myfile.dll
  de
    myfile.dll

How can I do this?

CodePudding user response:

use Link to change Output Path as follows, I also used Visible so that the changes don't appear in Solution Explorer:

<ItemGroup>
    <Content Include="Lib\sv\**">
        <Link>\sv\%(Filename)%(Extension)</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <Visible>false</Visible>
    </Content>
</ItemGroup>
<ItemGroup>
    <Content Include="Lib\de\**">
        <Link>\de\%(Filename)%(Extension)</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <Visible>false</Visible>
    </Content>
</ItemGroup>

CodePudding user response:

As I understand, msbuild will reflect the folder structure of the included contents into the output path and this cannot be altered.

I think your only option is to manually fix the output folder to match what you want. On windows you could use

move bin\Debug\net5.0\Lib\sv bin\Debug\net5.0
move bin\Debug\net5.0\Lib\de bin\Debug\net5.0

There are many ways of automating this in your solution. One way is to use Post Build Events under project properties -> Build -> Events. It should add the following to your csproj

 <Target Name="PostBuild" AfterTargets="PostBuildEvent">
     <Exec Command="move bin\Debug\net5.0\Lib\sv bin\Debug\net5.0&#xD;&#xA;move bin\Debug\net5.0\Lib\de bin\Debug\net5.0" />
 </Target>
  • Related