How to publish additional files into bin folder

Viewed 7856

My .net web app project also includes some unmanaged dlls as additional files. These are a couple of levels deep in subfolders.

When I publish this project I need these files to be copied to the bin folder alongside all the other binaries.

No matter what settings I try, the best I can get is for them to be published into their existing folder structure which is not where I need them to be.

I've created a PostBuild event to copy the files and this works when building locally but not when publishing to a server. I've not been able to get PostPublish events to work in the same way.

Is there another way to achieve this?

Note this is similar but not the same as a previous question: Publish unmanaged DLL from referenced project

3 Answers

I have a similar setup. 2 projects in my solution, one .NET Core and the other C++. When I am going to publish the dotnetcoreapp2.2 I want to include the precompiled C++ DLL from the other project. @JuanR's answer is not working for me, though it is already pretty close to my version. It looks like the <ItemGroup> needs to be in the <Target> tag.

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
  <ItemGroup>
    <DataModelFiles Include="$(ProjectDir)..\MyCppProject\bin\Release\MyCppProject.dll" />
  </ItemGroup>
  <Copy SourceFiles="@(DataModelFiles)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
</Target>

Try using an after-publish task.

You can create an item group for copy:

<ItemGroup>
  <binFilesToCopy Include="$(OutDir)\somepath\to\yourexternalDLLFolder\*" />
  <!-- Add more folders/files you want to copy here -->
</ItemGroup>

Then add a target for after publishing:

<Target Name="AfterPublish">
    <Copy SourceFiles ="@(binFilesToCopy)" DestinationFolder ="$(OutDir)\bin" />
</Target>

I did this mostly from memory so double-check for syntax, but get you the idea.

In the properties of the file you can set Copy to output directoryto Copy always or you can edit the solution file, expand the xml tag of the file needed and add <CopyToOutputDirectory>Always</CopyToOutputDirectory> as sub-tag.

Related