Maestro
Maestro

Reputation: 9518

Determine assembly location

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?

Upvotes: 0

Views: 164

Answers (2)

wal
wal

Reputation: 17759

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

Upvotes: 0

planestepper
planestepper

Reputation: 3317

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);

Upvotes: 1

Related Questions