How to add a VS assembly reference without hardcoding its path

Note: this entry has moved.

Sometimes, you want to add a reference to an assembly provided by VS (that is, something inside the %ProgramFiles%\Microsoft VisualStudio 8.0 folder) that doesn't show up in the Add Reference dialog (there are lots of very interesting stuff you can use that fall under these category, such as Microsoft.Data.ConnectionUI which I used in the GAX TechEd HoL and allows you to create an "Add Data Connection" clone complete with support for .NET providers, connection string builders, test connection, etc.).

The trick is to browse for the reference as usual using the Add Reference dialog, save the project and open it in notepad (or your favorite XML editor). Look for your reference, and replace the hardcoded path to the IDE folder with the $(DevEnvDir) macro variable, like so:

<Reference Include="Microsoft.Data.ConnectionUI, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>$(DevEnvDir)Microsoft.Data.ConnectionUI.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
  <Private>False</Private>
</Reference>

The  $(DevEnvDir) variable points to the %ProgramFiles%\Microsoft VisualStudio 8.0\Common7\IDE folder, but you can always use relative path “movements” to go up and around other folders under the root VS install path.

Yes, I know… VS should do that automatically… Go vote the bug so that we have it fixed in Orcas ;-)

Update: I’ve just found that you can also use $(VsInstallDir), which points to  %ProgramFiles%\Microsoft VisualStudio 8.0

2 Comments

  • I have a very related question for you. We use a lot of third party assemblies (no source), and we use file reference to reference these assemblies. The problem that we have is similar. All the team members need to have a folder that is similar otherwise the projects break. In VSTS, since we have a workspace concept, is there a way that I can use a variable like $(VSTSWorkspace) so that a referenced assembly would have a hintpath relative to each developer's workspace. Please let me know.

  • I'm a little lost. If your reference is to some folder within the TFS project's workspace, you can surely use a relative path from the VS project's location, can't you?



    And there're no variables set by TFS, nope.

Comments have been disabled for this content.