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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@Echo Off :: Globals set "fileMoveList=Syncfusion.Grid.Base,Syncfusion.Grid.Windows,Syncfusion.Licensing,Syncfusion.Shared.Base,Syncfusion.Shared.Windows,Syncfusion.SpellChecker.Base,Syncfusion.Tools.Base,Syncfusion.Tools.Windows,EnvDTE,stdole" :: 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 :NextItem if "%fileMoveList%" == "" goto :DeleteFiles For /F "tokens=1* delims=," %%a in ("%fileMoveList%") do ( if exist %%a.xml ( echo Deleting: %%a.xml del %%a.xml ) echo Moving: %%a.dll move %%a.dll "Multi Monitor Manager/lib" set "fileMoveList=%%b" ) goto :NextItem :DeleteFiles del "Multi Monitor Manager.pdb" del "Multi Monitor Manager.dll.config" Pause Exit |
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:
1 |
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve |
and related method:
1 2 3 4 5 6 7 8 9 10 |
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 Return Assembly.LoadFrom([module].FullName) End If Return Nothing End Function |
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:
Leave a Reply