Alternative location for references in .net projects

This article covers how to setup your .net project to consume its libraries from a location different to the root of the .exe

Normally, when you build a Visual Studio project, all the library .dlls are built to the application root folder. This can prove unsightly and ugly. If you’re making plugins, this can also junk up other peoples’ tidy dll structures! Here’s how to store your dlls in a sub folder of your application root.

In this example, I was building a Class Library to the Plugins folder of another’s application. Here’s what it looked like on build:

My Plugin was “Multi Monitor Manager” – all the syncfusion .dlls are mine. I wanted to put all my libraries in “Multi Monitor Manager/lib” apart from the Multi Monitor Manager.dll

Step 1: Setup a Post Build Event in Visual Studio

This will ensure the desired directory exists for your .dlls. I also set mine to delete the associated .xml files. It will move all the relevant .dlls form the root to the desired sub folder. N.b: the lib folder MUST be a subfolder of the application/plugin/library root folder.

Just test with a batch script first, and once you have it up and running, transfer it to you post-build event. My batch file looked something like this:

Just list all the libraries you want moving in fileMoveList

Step 2: Tell your code where to look for libraries if it doesn’t find them in the application root folder

At your application’s entry point, add the following:

and related method:

If you wanted more than one alternative lib location, you could just use a list of locations and loop through them.

And that’s it! The Plugin folder afterwards:

About stigzler 42 Articles
Chief crook and wattle bosher.

Be the first to comment

Leave a Reply

Your email address will not be published.


*