Neutrosider
Neutrosider

Reputation: 572

Set path of dlls referenced by another library

I have a C# project, a class library, wich is a module for another project, and it's output path is in a subfolder within the main-projects output path (i.e. outputpath = C:\WHATEVER\MainProject\Modules.

In this Project, i reference a library (let's call it a.dll). this in itself works fine, but this a.dll needs other libraries at runtime to work (let's call them b.dll and c.dll). I added these two files via Add -> Existing Item, and set "Copy to Output Directory" to "Copy always" for b.dll and c.dll.

When i build the project, all three dlls are copied over into the output-path of the project (as expected), but a.dll can't seem to find them. As soon as i copy b.dll and c.dll into the main-projects folder (where the .exe is) a.dll can suddenly find them.

I don't want to have b.dll and c.dll in the main-projects folder, because they are only part of the module i'm currently developing, and not the main-project itself. How (in Visual Studio 2015) do i tell a.dll where to look for b.dll and c.dll? I tried adding the probing-part in the appconfig (as suggested here: Reference a DLL from another DLL), but that had no effect. I do not have the sourcecode for a.dll.

Upvotes: 1

Views: 3000

Answers (1)

Steffen Winkler
Steffen Winkler

Reputation: 2864

The first part of this answer is based on what can be found here: https://stackoverflow.com/a/1892515/937093

The method being used is basically the same as the one referenced in what you linked, but instead of using the XAML method, it uses the C#/code method (easier to debug/figure out what is going on).

Add your custom path as described in that answer. Do that in the Main method of your application. Start by adding the relative path to that folder, if that doesn't work, take a look here: explanation of fuslogvw, to figure out where your program is looking, maybe a path is wrong or you don't have permission to look into that specific directory.

If all fails and/or you are getting nowhere here, you can also try the following:

In C#/.NET you've an event AppDomain.CurrentDomain.AssemblyResolve, you can read about it here.

In short, that even fires if .NET can't find an assembly and the eventhandler of that event allows you to resolve an assembly for .NET.

Inside the event you could do something like this: AssemblyName reqAssemblyName = new AssemblyName(args.Name);

if (reqAssemblyName.Name.ToLowerInvariant() == "b")
{
    return Assembly.LoadFrom(pathToAssemblyB);
}
else if (reqAssemblyName.Name.ToLowerInvariant() == "c")
{
    return Assembly.LoadFrom(pathToAssemblyC);
}
else
{
   return null;
}

To get the path to the two assemblies you can use Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mysubfolder","b.dll");

Upvotes: 1

Related Questions