Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Custom Path to Referenced DLL's?

People also ask

How do I reference a DLL in C #?

Copy the downloaded DLL file in a custom folder on your dev drive, then add the reference to your project using the Browse button in the Add Reference dialog. Be sure that the new reference has the Copy Local = True .

How do I find the path of a DLL?

If the DLL is listed in the Windows registry in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\KnownDLLs key, Windows searches for the DLL in the following locations in order: The %SystemRoot%\SYSTEM32 directory. The .exe file directory. The current directory.


From this page (untested by me):

Somewhere in your program's initialization (before you access any classes from a referenced assembly) do this:

AppDomain.CurrentDomain.AppendPrivatePath(@"bin\DLLs");

Edit: This article says AppendPrivatePath is considered obsolete, but also gives a workaround.

Edit 2: Looks like the easiest and most kosher way to do this is in the app.config file (see here):

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="bin\DLLs" />
    </assemblyBinding>
  </runtime>
</configuration>

From Tomek answer at: Loading dlls from path specified in SetdllDirectory in c#

var dllDirectory = @"C:/some/path";
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + dllDirectory)

It works perfectly for me!


Here is an other way to proceed without using obsolete AppendPrivatePath. It catches a kind of event "associated dll not found" (so it will be called only if the dll is not found in the default directory).

Works for me (.NET 3.5, not tested other versions)

/// <summary>
/// Here is the list of authorized assemblies (DLL files)
/// You HAVE TO specify each of them and call InitializeAssembly()
/// </summary>
private static string[] LOAD_ASSEMBLIES = { "FooBar.dll", "BarFooFoz.dll" };

/// <summary>
/// Call this method at the beginning of the program
/// </summary>
public static void initializeAssembly()
{
    AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args)
    {
        string assemblyFile = (args.Name.Contains(','))
            ? args.Name.Substring(0, args.Name.IndexOf(','))
            : args.Name;

        assemblyFile += ".dll";

        // Forbid non handled dll's
        if (!LOAD_ASSEMBLIES.Contains(assemblyFile))
        {
            return null;
        }

        string absoluteFolder = new FileInfo((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).LocalPath).Directory.FullName;
        string targetPath = Path.Combine(absoluteFolder, assemblyFile);

        try
        {
            return Assembly.LoadFile(targetPath);
        }
        catch (Exception)
        {
            return null;
        }
    };
}

PS: I did not managed to use AppDomainSetup.PrivateBinPath, it is too laborious.