7

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

userSteve
  • 1,554
  • 1
  • 22
  • 34
  • 2
    Possible duplicate of [How to include other files to the output directory in C# upon build?](https://stackoverflow.com/questions/16785369/how-to-include-other-files-to-the-output-directory-in-c-sharp-upon-build) – JuanR Sep 19 '18 at 15:42
  • 1
    @JuanR Similar but not the same. That question is referring to the output directory and build events, I am specifically interested in the bin directory and publishing (build events don't fire on publish) – userSteve Sep 19 '18 at 15:45
  • I see. Have you tried using a post-publish task instead? https://stackoverflow.com/questions/15090558/post-build-event-command-for-publish-visual-studio-2010 – JuanR Sep 19 '18 at 16:00

3 Answers3

8

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>
asdf
  • 952
  • 14
  • 13
  • Worked like a champ. With the addition of there is a lot of flexibility. I have global .json files that I am now able to copy in on build and on publish. I also have dev files and a secrets file that that only are applicable when running locally. – drewid Dec 06 '20 at 04:21
4

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.

JuanR
  • 7,405
  • 1
  • 19
  • 30
1

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.

Tobias Brohl
  • 479
  • 6
  • 25
  • Unfortunately this copies the whole folder structure to the bin folder, I just need the files themselves to be in the root of bin not in any subfolders – userSteve Sep 19 '18 at 16:12
  • @userSteve is it possible for you to put the file in the root directory of the solution – Tobias Brohl Sep 19 '18 at 16:13
  • 1
    it's not ideal. I have 12 of the files, it would be rather messy to have them hanging around in the root of the project. The folder structure they are in now is neat and explains what they are for – userSteve Sep 19 '18 at 16:20
  • @userSteve I don't know any other way to do that, which would work reliable for remote building – Tobias Brohl Sep 19 '18 at 16:24
  • Its just a shame the postbuild events work fine to copy them, postpublish doesnt work the same way – userSteve Sep 19 '18 at 16:44
  • @userSteve: Did you see my comment about post-publish **tasks**? It's not the same as events. – JuanR Sep 19 '18 at 17:37
  • @JuanR I think I've tried that before. It requires a .bat file to be included in the project too. Don't think it likes the $TargetDir syntax like PostBuild. I will try again anyway. Thanks – userSteve Sep 19 '18 at 17:41
  • 1
    @userSteve: Not necessarily. There are built-in tasks to copy files, no bat file required. You should be able to use the bin folder as a target. – JuanR Sep 19 '18 at 18:01