How to copy dependent assemblies to the bin folder on build?

This post was most recently updated on April 28th, 2022.

2 min read.

This is something that comes up pretty often – when you’re building an application, or maybe a library you’re sharing as a NuGet package to your friends: You’ll need to share a dependency or two with your particular package, and you don’t want your end users having to reference those libraries, too.

Sounds easy, right? But alas, it’s not as simple as one would like :)

(Well, unless you’re happy running dotnet publish every time – that should work, but I want to have these files available right after dotnet build instead)

Problem

Table of Contents

Sounds easy, right? There’s a problem, though – you can’t just set “Copy Local” to “true” anymore. That’s simply not available for your dependencies.

And forget all of that <IncludeAssets> nonsense inside your PackageReferences – probably not going to help either.

At least I was getting a bit frustrated. Couldn’t find a way to include the DLLs without building some super questionable build conditions and/or scripts. Or, well, running dotnet publish all the time.

However, there’s another solution available!

Solution

Let’s get to it, then!

Time needed: 2 minutes.

How to copy referenced assemblies to the bin folder on build?

  1. Open your project file

    Simple enough, just select “Edit Project File” by clicking your project using the right mouse button in the Solution Explorer.

    "Edit Project File" in Visual Studio Solution Explorer

  2. Add CopyLocalLockFileAssemblies to your .csproj file

    This to your <PropertyGroup>:

    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

    This will copy all of the NuGet dependencies to your output directory! The documentation says this:
    If you set this property to true, any NuGet package dependencies are copied to the output directory. That means you can use the output of dotnet build to run your plugin on any machine.

  3. Reload project and rebuild

    … and you should be good!


And that’s it!

How did it go for you? Let me know in the comments section below!

References

mm
5 1 vote
Article Rating
Subscribe
Notify of
guest

4 Comments
most voted
newest oldest
Inline Feedbacks
View all comments