tags:

views:

149

answers:

5

Normally you have to put all the .dll files your application uses in the same location as the .exe file. I would like to keep the libraries in some other, dedicated folder and just show my app where to look for them (by some path in app.config or something like that). How do I do this?

+5  A: 

You can use the <probing> element in app.config to specify where to look for dependencies. I believe this will have to be a directory under the location of the exe though, not in an arbitrary place on disk.

See "Locating the assembly through codebases or probing" for more information on how this hangs together.

Jon Skeet
well i need some other place on disk, not an application's directory subdirectory
agnieszka
In that case I think your choices are 1) the GAC; 2) Assembly.LoadFrom (i.e. doing it manually); 3) Investigate the codeBase element, but I'm not sure that will cover your situation.
Jon Skeet
Does <probing> work with linked directories, i.e. create a subdirectory that is actually a link (soft or hard) to the actual directory. This is quite hacky, of course :-)
Per Erik Stendahl
A: 

You could use Reflection to load in the dll from a specified path and then access all the classes and methods therein, although it would be a lot of hard work! You would also lose the benefits of intellisense, however it is possible, see:

http://www.codeproject.com/KB/cs/csharpreflection.aspx

Calanus
+2  A: 

Consider also the GAC - its purpose is to hold assemblies ;)

Oliver Hanappi
but gac belongs to the computer, so i would have to put the assembly to the computer's gac on every computer that uses the app?
agnieszka
A: 

Actually there is some other way that suits my needs best. Using reflection (assebly.loadfrom) is unpleasant and would require a lot of changes in my code. But you could add a handler to appDomain.AssemblyResolve event and show the location of requested assembly if it is not found.

static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            return Assembly.LoadFrom("C:\\TestAssembly.dll");
        }
agnieszka
I think this would lead to performance issues as it run time has will invest some cpu time in search of dlls
Uday
A: 

I advise you to either use the GAC or distribute all dependent DLLs with the EXE. Otherwise you risk getting obscure versioning bugs or applications that stop working suddenly and what-not.

Also, don't forget that if your shared code is on a network share you'll probably need to modify each computers security settings anyway.

Regards

Per Erik Stendahl
see the comment below oliver hannapi's answer
agnieszka