views:

1403

answers:

3

My DLLs are loaded by a third-party application, which we can not customize. My assemblies have to be located in their own folder. I can not put them into GAC (my application has a requirement to be deployed using XCOPY). When the root DLL tries to load resource or type from another DLL (in the same folder), the loading fails (FileNotFound). Is it possible to add the folder where my DLLs are located to the assembly search path programmatically (from the root DLL)? I am not allowed to change the configuration files of the application.

+2  A: 

You can add a probing path to your application's .config file, but it will only work if the probing path is a contained within your application's base directory.

Mark Seemann
+2  A: 

Sounds like you could use the AppDomain.AssemblyResolve event and manually load the dependencies from your DLL directory.

Mattias S
Thank you, Mattias! This works: AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolderResolveEventHandler); static Assembly LoadFromSameFolderResolveEventHandler(object sender, ResolveEventArgs args) { string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string assemblyPath = Path.Combine(folderPath, args.Name + ".dll"); Assembly assembly = Assembly.LoadFrom(assemblyPath); return assembly; }
Valery Tydykov
A: 

look into AppDomain.AppendPrivatePath (deprecated) or AppDomainSetup.PrivateBinPath

Vincent Lidou