Probing Working in Debug Folder but Nowhere Else

1k views Asked by At

This is my App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="lib" />
      </assemblyBinding>
    </runtime>
</configuration>

My referenced DLLs are set to not copy to local and I've copied the DLLs to the lib directory in the debug folder and it works well, but when I move my program along with my library folder it doesn't work.

Is there a way I could make it work when the program and library directory to a different folder?

1

There are 1 answers

0
s1cart3r On

A bit late to this one, but there is an alternative, add this to your Main method

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;

The create the method

    private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args)
    {
        AssemblyName assyName = new AssemblyName(args.Name);
        string filename = args.Name.ToLower().Split(',')[0];
        string path = "c:\some path or other\"
        string assembly = System.IO.Path.Combine(path, filename);

        if (!assembly.EndsWith(".dll"))
            assembly += ".dll";

        try
        {
            if (System.IO.File.Exists(assembly))
                return (Assembly.LoadFrom(assembly));
        }
        catch (Exception error)
        {
            // do whatever
        }

        return (null);
    }