Tamara
Tamara

Reputation:

How to set C# library path for an application?

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?

Upvotes: 13

Views: 21227

Answers (7)

Robert Baker
Robert Baker

Reputation: 25963

It's possible without the GAC, but assemblies must be strong named and you must make changes to app.config whenever the version or publickeytoken changes. This is what I have Main Program in \, Shared libs in \Shared. and a sub program I want separated in \SDK, it uses ..\Shared for the assemblies.

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="shared" />
      <dependentAssembly>
         <assemblyIdentity name="protobuf-net" publicKeyToken="257b51d87d2e4d67" />
         <codeBase version="1.0.0.282" href="../protobuf-net.dll"/>
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="SevenUpdate.Base" publicKeyToken="5d1aea1de74f122c" />
         <codeBase version="11.5.4.0" href="../SevenUpdate.Base.dll"/>
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="SharpBits.Base" publicKeyToken="5d1aea1de74f122c" />
         <codeBase version="11.5.5.0" href="../SharpBits.Base.dll"/>
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="System.Windows" publicKeyToken="5d1aea1de74f122c" />
         <codeBase version="11.5.5.0" href="../System.Windows.dll"/>
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="WPFLocalizeExtension" publicKeyToken="5d1aea1de74f122c" />
         <codeBase version="11.5.5.0" href="../WPFLocalizeExtension.dll"/>
       </dependentAssembly>
    </assemblyBinding>
  </runtime>

Upvotes: 1

GvS
GvS

Reputation: 52518

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

Upvotes: 8

user76035
user76035

Reputation: 1536

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

Upvotes: 4

Richard
Richard

Reputation: 109005

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500515

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.

Upvotes: 2

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

Like I said in my answer on your previous question:

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

Upvotes: 0

Related Questions