3

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 ?

4

2 Answers 2

5

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

2
  • okay... so far its okay. But what is my config file ? i only can find a app.config...
    – Dominik
    Jul 11, 2012 at 9:08
  • If you right click on a project in your solution Add => New Item => General => application Configuration file. Then just add the line I posted between the <configuration> . google Application Configuration file for more info.
    – MBen
    Jul 11, 2012 at 9:15
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.