Reputation: 299
I have an C#-application, that uses another C# DLL. If I use the exe file it works, when the DLL-File is in the same directory as the exe-File. But know I would create a folder and put the dll-File in it. In the MSDN Help found something that should work but i doesn´t why?
How can I load the DLL out of the subfolder ?
Upvotes: 3
Views: 3974
Reputation: 626
If you want more flexibility, you can simple handle the assembly resolving yourself. This way you have full control over which code gets loaded. Here's some code from one of my projects.
'new AssemblyName(args.Name)' gives an object that you can use to get info about the assembly required, the rest of the code loads the assembly from an embedded file. If you want to load the assembly from a subfolder, you can just use Assembly.Load().
private static void InstallAssemblyResolveHandler()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
String resourceName = "AssemblyLoadingAndReflection." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
}
Upvotes: 0
Reputation: 3996
You need to add the folder you want to load the dll from to your config file :
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="YourSubFolderHere;SubFolderTwo"/>
</assemblyBinding>
</runtime>
Or you can do it through code using :
AppendPrivatePath
more about the probing path in msdn
Upvotes: 5