16

I have compiled my project and some of my project's added .dlls have absolute references. When I try to run my project on another machine, it looks for the .dlls from the original project path.

How can I make the project look for the .dlls using a relative path?

alex
  • 6,818
  • 9
  • 52
  • 103
Oded .S
  • 1,081
  • 2
  • 11
  • 18

2 Answers2

19

Edit the .csproj file and change the <HintPath> elements from absolute paths to relative paths.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    When I was adding DLL references I noticed it automatically used relative paths. In what instances would Visual Studio have used absolute paths? – Ciaran Gallagher Dec 09 '13 at 14:04
  • 8
    It uses absolute paths when the assembly you are referencing is above the root of the project. – Jason Kelley Jun 09 '14 at 17:51
  • I just checked and **even if** the assembly is above the root of the project, VS 2010 adds relative paths automatically. – B Charles H Sep 27 '18 at 11:11
4

You may also write your handler for resolving assemblies. In the simplest form it may look like this:

AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler;
..
static Assembly AssemblyResolveHandler(object sender, ResolveEventArgs args)
{
  string assemblyPath = "yourpath";
  return Assembly.LoadFrom(assemblyPath + args.Name);
}

Another option is adding entry in App.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="yourpath"/>
     </assemblyBinding>
  </runtime>
pkmiec
  • 2,594
  • 18
  • 16