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

This post was most recently updated on November 2nd, 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

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

The solution includes modifying your .csproj file (because of course it does!) – let’s see how:

Time needed: 5 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

    Okay – so you need to add 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

    After reloading and rebuilding 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 3 votes
Article Rating
Subscribe
Notify of
guest

11 Comments
most voted
newest oldest
Inline Feedbacks
View all comments