views:

1430

answers:

6

Hello, I have C# application that uses a dll. When I try to run the application, it can't find the dll, unless it is in the same directory or in GAC. I do not want to have it in the same directory and I do not want to install it to GAC. Is there any way how to tell the application where to look for the library? (For example if I want to distribute the application to customers and they want to use their own applications that would use the dll.)

Added:

I would like to have this file structure:

MainFolder: Libraries, Applications

Libraries: lib.dll

Applications: app1.exe

I don't want to copy it to GAC or have lib.dll in folder Applications. Is it possible?

+5  A: 

Follow this guide on MSDN: Specifying an Assembly's Location

Mehrdad Afshari
Is'n this basically the same as copying to GAC?
No, they are conceptually different. You can even reroute an assembly installed in GAC to another version by this method. You have to edit machine configuration or put the DLL in a subdirectory. I doubt any of them is what you want, but as GvS and Jon pointed out, this is not a good thing in general
Mehrdad Afshari
A: 

Like I said in my answer on your previous question:

Use Assembly Redirection instructions in your app.config or machine.config.

Gerrie Schenck
+5  A: 

I would recommend that the applications of your customers copy the dll's they use in their own directory.

VB6 used to share dll's between applications, we have a term for this: DLL Hell

GvS
+1 Great answer! I have to upvote!
Mehrdad Afshari
Please use the more modern term for "DLL Hell" ---> The Global Assembly Cache. =) I think MS meant well ("The road to hell is...") when they made the GAC, but it feels like the same old pain... app weirdness, missing functions, extra deployment steps, etc.
StingyJack
+1  A: 

The DLL will have to either be in the GAC or in the application's directory or a subdirectory, as the answers to your earlier question said.

If your customers want to write their own applications using the DLL, you should either install it in the GAC or get them to copy the DLL too. Having multiple copies of the library doesn't sound like a good thing, but it really is: it means you can upgrade one copy to a different version without breaking everything else.

Jon Skeet
A: 

You can log, and then view the actions the framework took trying to load an assembly. This makes diagnosing assembly load errors very easy.

The tool to do both is "FUSLOGVW.exe" (Fusion Log Viewer, Fusion being the name of the loader) and included in the SDK.

Richard
+1  A: 

In your Main:

 AppDomain.CurrentDomain.AssemblyResolve += (s,e)=>{
    var filename = new AssemblyName(e.Name).Name;
    var path = string.format(@"C:\path\to\assembly\{0}.dll",  filename);
    return Assembly.LoadFrom(path);
 };

Add some exception handling to that