Visual Studio: Build DLLs to a separate folder

When building a project in Visual Studio, your application’s folder can quickly get out of hand if you have several assemblies and related files. You can move the assemblies to a subfolder and keep your application’s root folder clean.

Specify subdirectories for searching assemblies

The first step is to specify additional paths for searching assemblies. You can do that in your App.config file.

Here we specify lib as an additional folder:

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="lib" />

You can provide more than one path by delimiting with semicolons.

Edit post-build events

Now we will add some post-build events so the assemblies are moved to the correct folder.

Go to the project properties and then Build Events.

Post-build events
Post-build events

These are the commands we specified:

; Move all assemblies and related files to lib folder
ROBOCOPY "$(TargetDir) " "$(TargetDir)lib\ " /XF *.exe *.config *.manifest /XD lib logs data /E /IS /MOVE
if %errorlevel% leq 4 exit 0 else exit %errorlevel%

We are using Robocopy to move the files, which is included in Windows since Windows Vista.

Of note:

  • XF: exclude files (here we don’t move *.exe, *.config or *.manifest files)
  • XD: exclude folders (here we don’t move lib, logs and data subfolders)

Important: notice the spaces before closing quotes. This is needed because Robocopy treats backslash as an escape character and $(TargetDir) includes one at the end.

The last line is needed because Robocopy has several return codes and the build event will fail if the exit code is not 0.

Related articles