0

I have project called A, located in C:\ProjectA. It references a dll called B.dll, located in C:\Binaries.

Now B.dll has to dynamicly load a second DLL called C.DLL which is in the same folder (C:\Binaries). But how can B determine C's location?

I know about AppDomain.CurrentDomain.BaseDirectory and Assembly.GetExecutingAssembly().Location, but both will return 'C:\ProjectA\', because B.dll was loaded by A.exe.

I know the obvious solution would be to place all binaries in the same folder, and they will be when released, but while developing I cannot change the repositry's layout, and I want to avoid to hardcode the paths.

Edit: Sorry duplicate of How do I get the path of the assembly the code is in?

Community
  • 1
  • 1
Maestro
  • 9,046
  • 15
  • 83
  • 116

2 Answers2

1

From MSDN, you have to test it based on some type existing in C (or B):

Assembly assembly = Assembly.GetAssembly(yourVar.GetType());
//your location will be in assembly.Location
Console.WriteLine("Location=" + assembly.Location);
planestepper
  • 3,277
  • 26
  • 38
0

How about using Assembly.GetCallingAssembly from B? This will return The Assembly object of the method that invoked the currently executing method. (ie B)

public void BMethod()
{
     var assembly = Assembly.GetCallingAssembly();
     string path = assembly.Location;
      //now use this path to load C.dll in the same folder.

}

see also this similar stack overflow question

Community
  • 1
  • 1
wal
  • 17,409
  • 8
  • 74
  • 109