Set Custom Path to Referenced Dll'S

Set Custom Path to Referenced DLL's?

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>

Set path of dlls referenced by another library

The first part of this answer is based on what can be found here: https://stackoverflow.com/a/1892515/937093

The method being used is basically the same as the one referenced in what you linked, but instead of using the XAML method, it uses the C#/code method (easier to debug/figure out what is going on).

Add your custom path as described in that answer. Do that in the Main method of your application. Start by adding the relative path to that folder, if that doesn't work, take a look here: explanation of fuslogvw, to figure out where your program is looking, maybe a path is wrong or you don't have permission to look into that specific directory.

If all fails and/or you are getting nowhere here, you can also try the following:

In C#/.NET you've an event AppDomain.CurrentDomain.AssemblyResolve, you can read about it here.

In short, that even fires if .NET can't find an assembly and the eventhandler of that event allows you to resolve an assembly for .NET.

Inside the event you could do something like this:
AssemblyName reqAssemblyName = new AssemblyName(args.Name);

if (reqAssemblyName.Name.ToLowerInvariant() == "b")
{
return Assembly.LoadFrom(pathToAssemblyB);
}
else if (reqAssemblyName.Name.ToLowerInvariant() == "c")
{
return Assembly.LoadFrom(pathToAssemblyC);
}
else
{
return null;
}

To get the path to the two assemblies you can use Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mysubfolder","b.dll");

How do I set the path to a DLL file in Visual Studio?

  1. Go to project properties (Alt+F7)
  2. Under Debugging, look to the right
  3. There's an Environment field.
  4. Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
  5. Hit F5 (debug) again and it should work.

DLL reference location

When you add a reference in the way you've described it is copied to the output folder (same as the exe file). Look in the properties of the reference (F4) and you will see an option called "Copy Local", if this is set to true then the DLL will be copied to the same output folder as the EXE file.

Copy Local Setting

So when you deploy your application to another machine you will need to copy the exe and all it's referenced DLLs to the deployment location. Windows will search for DLLs in a number of locations, the first of which is the same folder as the EXE file.

Can I change the already referenced DLL's path?

If the path is already hard-coded, I can't see how you can change that without editing the code, but perhaps there is another alternative:

If you have the correct dll-file (correct name and version number as referenced by .net) in the Global Assembly Cache (the "GAC"), then there is a chance .NET might load it from there.

I'm saying it might do so, because I'm not sure about how this works if you're explicitly loading a dll directly from code. If the assembly is simply referenced by name / namespace in the normal way though, it should search for the library in the gac first.

May be worth a shot anyway. You'll find your GAC at: %windir%\Microsoft.NET\assembly

how to change dll directory in C# project

I wanted to change dll directory that users cant see them. So I try to load DLL from Embedded Resource. its a good idea that workes for me.

by this way, you can change your exe file with out any related dll.

see here for more detailes



Related Topics



Leave a reply



Submit