25

I have a .NET console app that references a DLL. It runs fine if the DLL is in the same folder as the EXE but I would like to put the DLL in a different folder. How can I do that?

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • possible duplicate of [How to add folder to assembly search path at runtime in .NET?](http://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net) – nawfal Feb 20 '13 at 11:28

3 Answers3

22

If the DLL is in a sub folder you can add this folder to the AppDomain private path.

The private bin path of an AppDomain cannot be changed once the AppDomain has been created (AppDomain.AppendPrivatePath is obsolete), if you don't want to create a new AppDomain, you can modify the probing element in the App.config.

If it's not in a sub folder, things get more complicated, you can load the assembly using its full path, but if it references other assembly, the CLR won't be able to resolve the dependencies.

If you want to resolve dependencies, you can add an handler to AppDomain.AssemblyResolve and fetch the needed assembly in your specific folder.

Another possibility is to place this DLL (and dependencies) in the GAC.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Jeff Cyr
  • 4,774
  • 1
  • 28
  • 42
  • 1
    But if you add the path to the process's own environment's PATH, then dependent libraries will be resolved. And there will be much rejoicing. – Seva Alekseyev Jan 05 '10 at 21:18
  • 1
    Depending on the environment PATH makes deployment harder, if it can be avoided you can gain much rejoicing ;) – Jeff Cyr Jan 05 '10 at 21:27
  • Document the `` element in the app.config file to make your post perfect. – Hans Passant Jan 05 '10 at 21:30
  • 3
    I'm talking about adjusting the private PATH of the process, not the systemwide PATH. I had to do it once so that a whole DLL tree of third-party libraries loads right. Otherwise, you can load one library by full path, but *its* dependencies from the same folder then fail to load. Just throwing 2-cent coins towards the OP here... – Seva Alekseyev Jan 05 '10 at 21:30
  • It seems like I can't do any of these things because the FileNotFound is thrown before Main even starts executing! – JoelFan Jan 05 '10 at 23:05
  • Where is the assembly you are trying to reference, is it in a sub folder of the folder containing the exe? FileNotFoundException would indicate that you reference the assembly explicitly in your project, you can't do this if the assembly isn't in a subfolder and the probing element is configured correctly. – Jeff Cyr Jan 06 '10 at 00:27
  • I added the reference in Visual Studio... it is not a subfolder of the exe. It works from Studio, but not stand-alone. – JoelFan Jan 06 '10 at 17:11
  • As for adding a handler to AssemblyResolve... I can't seem to find an example of how to do this in a way that I can also call the default handler (or allow it to run naturally) in most cases... just handle the specific DLLs I need to. – JoelFan Jan 06 '10 at 17:13
  • Then you can't reference the assembly explicitly without setting Copy Local to true in the reference property page. Maybe you should give more detail about your context in your question if you want an answer. – Jeff Cyr Jan 06 '10 at 17:57
2

If the DLL is loaded dynamically, just specify the full path to it. If it's static, or you feel like it, add the library path to the PATH environment variable. Yes, this DOS relic is still around.

Also, read the doc on the LoadLibrary() API for a better understanding of DLL loading.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

Well you could have the application look in certain directories that you can specify in a configuration section. Then you could load them up at the start of your application. I don't really know what you are trying to do, but this could be a solution.

Assembly.Load("Assembly.Name")

If you aren't building some kind of plugin architecture, I would think about why you have to do this.

Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75