Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a path(s) to the DLL search order

I have a .NET application which I wish to search other paths for dependent DLLs besides the standard GAC, current directory, PATH areas. Is it possible to tell the app to do this?

E.g.

Tell the Application to look in "[Executable Path]\Dependent DLLs".

like image 398
Keith Adler Avatar asked Jul 26 '11 21:07

Keith Adler


People also ask

What is DLL search path?

If the DLL is listed in the Windows registry in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\KnownDLLs key, Windows searches for the DLL in the following locations in order: The %SystemRoot%\SYSTEM32 directory. The .exe file directory. The current directory.


2 Answers

You can add a private assembly search path by using the <probing> element in your .config file.

like image 117
Julien Lebosquain Avatar answered Sep 21 '22 10:09

Julien Lebosquain


An alternative approach to setting probing in you config (if you require more flexibility) - is that you can add your own handler to search other locations and load them yourself.

In the main method that starts your app

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;

add a handler with your own method to take the name and search other locations -

private static Assembly CurrentDomainAssemblyResolve(object setnder, ResolveEventArgs args)
{
     return LoadEmbeddedAssembly(args.Name);
}

I use this method to embed DLLs as resources so I can have a single executable to distribute, but you can load them from a directory just as well.

like image 45
bkr Avatar answered Sep 18 '22 10:09

bkr