106

When adding a reference to an assembly located within the solution directory, is there any way to add it relatively, so that when checked in and out of a repository it is referenced in projects correctly?

theringostarrs
  • 11,940
  • 14
  • 50
  • 63
  • 14
    If it doesn't quite work from the UI, remember that you can just hand-edit the .proj file directly and change the path to relative. Once you change it either way, it stays the way you've put it. – Pavel Minaev Jul 31 '09 at 05:05
  • 1
    In Visual Studio 2010 reference paths are stored as relative by default, so if this is happening to you something else is wrong. In my case it was I'd blanket ignored dlls from version control so I could compile the solution but my workmates couldn't. – Giles Roberts Apr 16 '13 at 15:34

6 Answers6

133

To expand upon Pavel Minaev's original comment - The GUI for Visual Studio supports relative references with the assumption that your .sln is the root of the relative reference. So if you have a solution C:\myProj\myProj.sln, any references you add in subfolders of C:\myProj\ are automatically added as relative references.

To add a relative reference in a separate directory, such as C:/myReferences/myDLL.dll, do the following:

  1. Add the reference in Visual Studio GUI by right-clicking the project in Solution Explorer and selecting Add Reference...

  2. Find the *.csproj where this reference exist and open it in a text editor

  3. Edit the < HintPath > to be equal to

    <HintPath>..\..\myReferences\myDLL.dll</HintPath>

This now references C:\myReferences\myDLL.dll.

starball
  • 20,030
  • 7
  • 43
  • 238
CrimsonX
  • 9,048
  • 9
  • 41
  • 52
  • 22
    I understand that this is helpful and correct, but does the VS gui *seriously* not provide a way to add a relative reference? – kdbanman Jul 08 '15 at 21:52
  • 4
    In VS 2015 I got this behavior by default (relative path) – Mugen Jan 13 '16 at 07:09
  • 2
    I was convinced that it had added an absolute path, till I followed your directions out of curiosity. VS showed an absolute path in the reference properties, but showed as already relative. Turns out I was missing files O_o lolimaderp! – donutguy640 Dec 06 '17 at 01:35
  • @donutguy640 That got me confused too. They should also show the relative path on the properties pane – Alexander Derck Aug 09 '18 at 09:11
  • 1
    You don't have to directly edit the project file. Select "browse" in the add reference dialog, navigate to the folder with your project file, then add the relative path to your DLL. Although an absolut path is displayed in the properties window, the relative path is saved to the project file. VS really should display the path just like it is saved in the project file. – needfulthing Mar 19 '19 at 16:46
9

Yes, just create a directory in your solution like lib/, and then add your dll to that directory in the filesystem and add it in the project (Add->Existing Item->etc). Then add the reference based on your project.

I have done this several times under svn and under cvs.

Freddy
  • 3,064
  • 3
  • 26
  • 27
  • 5
    You don't need to add the dll to the project itself, just add reference to it. The best thing to do is to add the whole 'lib' directory to your source control. See http://code.google.com/p/projectpilot/source/browse/#svn/trunk as an example – Igor Brejc Jul 31 '09 at 05:02
  • 1
    You are right. I checked an old project and neither the directory or the dlls were added to the project itself, just to the repository. And the reference is then relative to the project. Sorry about that. – Freddy Jul 31 '09 at 05:08
5

In VS 2017 it is automatic. So just Add Reference as usually.

Note that in Reference Properties absolute path is shown, but in .vbproj/.csproj relative is used.

<Reference Include="NETnetworkmanager">
      <HintPath>..\..\libs\NETnetworkmanager.dll</HintPath>
      <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
Yuriy Vasylenko
  • 3,031
  • 25
  • 25
2

Probably, the easiest way to achieve this is to simply add the reference to the assembly and then (manually) patch the textual representation of the reference in the corresponding Visual Studio project file (extension .csproj) such that it becomes relative.

I've done this plenty of times in VS 2005 without any problems.

Uwe Honekamp
  • 2,478
  • 1
  • 17
  • 15
  • 3
    I think it is not necessary to modify the project file manually. In my experience Visual Studio always uses relative pathes. The only time I had to modify the project file by hand was when I wanted to share a key file (.snk). Visual Studio always just copies the key file into the project directory which results in several copies of the key file. – Stefan Egli Jul 31 '09 at 07:27
1

I might be off here, but it seems that the answer is quite obvious: Look at reference paths in the project properties. In our setup I added our common repository folder, to the ref path GUI window, like so

Reference Paths in VS20xx

That way I can copy my dlls (ready for publish) to this folder and every developer now gets the updated DLL every time it builds from this folder.

If the dll is found in the Solution, the builder should prioritize the local version over the published team version.

Christian
  • 394
  • 2
  • 17
  • Unfortunately this is kept in the csproj.user file and is not relative. – Jamie Kitson Apr 24 '20 at 12:54
  • By my expectations, the path specified in "Reference paths" should act as an additional search path for referenced DLLs when the app is started from VS in debugger by F5. But it is not. – Vlad Gonchar Mar 05 '22 at 16:53
0

As mentioned before, you can manually edit your project's .csproj file in order to apply it manually.

I also noticed that Visual Studio 2013 attempts to apply a relative path to the reference hintpath, probably because of an attempt to make the project file more portable.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54