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:
:: First check Folder Sturctures exist
if not exist "Multi Monitor Manager/" mkdir "Multi Monitor Manager"
if not exist "Multi Monitor Manager/lib/" mkdir "Multi Monitor Manager/lib"
:: Now move relevant libraries to lib folder and also delete any .xml metadata
if "%fileMoveList%" == "" goto :DeleteFiles
For /F "tokens=1* delims=," %%a in ("%fileMoveList%") do (
if exist %%a.xml (
echo Deleting: %%a.xml
echo Moving: %%a.dll
move %%a.dll "Multi Monitor Manager/lib"
del "Multi Monitor Manager.pdb"
del "Multi Monitor Manager.dll.config"
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:
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve
and related method:
Public Shared Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
Dim di = New IO.DirectoryInfo("Multi Monitor Manager/lib")
Dim [module] = di.GetFiles().FirstOrDefault(Function(i) i.Name = args.Name.Split(",")(0) & ".dll")
If [module] IsNot Nothing Then
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: