Search code examples
c#.netassembliesstrongname

Load multiple assemblies


How can I load an assembly using its full display name from a location outside the application's bin path?

Usually one can load an assembly from a custom location with

Assembly.LoadFrom(path);

This works, but it seems that for loading a strong-named assembly I need to specify its full display name such as in

Assembly myDll =
Assembly.Load("myDll, Version=1.0.0.1, Culture=neutral, PublicKeyToken=9b35aa32c18d4fb1");

But the problem here is that this only references assemblies that are in my probing path of my application.

So what if I have an assembly dir1/asm.dll and one assembly dir2/asm.dll and both have a strong name.

How can I load them during runtime?


Solution

  • During Runtime, you can specify additional directories to probe when loading an assembly via the following methods:

        AppDomain.CurrentDomain.ClearPrivatePath();
        AppDomain.CurrentDomain.AppendPrivatePath();
    

    When the subdirectory names are already known during installation, you can also specify these additional directories in the app.config file in the privatePath attribute of the <probing> element.

    Make also sure the file name is correct. When you have

    AppDomain.CurrentDomain.AppendPrivatePath("Subdir");
    
    Assembly myDll = Assembly.Load("myDll, Version=1.0.0.1, Culture=neutral, PublicKeyToken=9b35aa32c18d4fb1");
    

    then .net will look for a file named "mydll.dll" in the directory "Subdir" beneath the directory of the executable.