46

I've downloaded a number of 3rd party libraries (dlls) now for Visual Studio 2010/C# and I've noticed that in their distributions \bin directory they usually have two versions Debug and Release.

Is there a way to add these libraries as references to the project, but use the Release build (when I'm building a release), and use the Debug build (when I'm debugging)?

leeand00
  • 25,510
  • 39
  • 140
  • 297

4 Answers4

76
<Reference Include="MyLib">
   <HintPath>..\lib\$(Configuration)\MyLib.dll</HintPath>
</Reference>
WaffleSouffle
  • 3,293
  • 2
  • 28
  • 27
  • 1
    Definitely the best answer, works like a charm. I tried the one marked as correct but it did not work (I had two reference and one witth exclamation mark in VS2017) – Krypto_47 May 08 '18 at 12:15
42

You can edit the csproj file manually set the Condition attribute on the ItemGroup containing the reference.

  <ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Reference Include="MyLib">
      <HintPath>..\..\Debug\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Reference Include="MyLib">
      <HintPath>..\..\Release\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

See this article for a bit more information.

sergtk
  • 10,714
  • 15
  • 75
  • 130
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • 1
    @PHeilberg When I save my .csproj file it won't overwrite this right? – leeand00 Mar 30 '11 at 19:49
  • 1
    @leeand00: no, it will keep the settings provided you have loaded them in Visual Studio. If you change it with VS open it will prompt you to reload the solution in order to apply your changes. – PHeiberg Mar 30 '11 at 20:05
  • 5
    One thing that's a bit confusing with applying the condition attribute is that the reference will show twice in VS, but only the one fitting the current configuration will actually be applied. – PHeiberg Mar 30 '11 at 20:10
  • 1
    In some cases, an alternative change to the csproj could be inserting the Configuration variable in the HintPath for the dll, like: ..\..\$(Configuration)\MyLib.dll – Bovaz Mar 19 '18 at 10:17
12

The answer by WaffleSouffle is definitely the best if you use a Release- and a Debug-folder, as the original question states.

There seems to be another option that is not so obvious because VS (VS2010) does not show it in the IntelliSense when editing the csproj-file.

You can add the condition to the HintPath-element. Like this:

<Reference Include="MyLib">      
      <HintPath Condition="'$(Configuration)'=='Release'">..\lib\MyLib.dll</HintPath>
      <HintPath Condition="'$(Configuration)'=='Debug'">..\lib\Debug\MyLib.dll</HintPath>
</Reference>

I found an article by Vivek Rathod describing the above approach at http://blog.vivekrathod.com/2013/03/conditionally-referencing-debug-and.html.

I checked the XMS Schema file for the project file at: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Core.xsd and: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd

I cannot see that Condition is a supported attribute for the HintPath-element, but it does seem to work.....

EDIT 1: This does not make the reference show up twice in Visual Studio which is an issue with the accepted answer.

EDIT 2: Actually, if you omit the HintPath alltogether Visual Studio will look in the projects output folder. So you can actually do this:

<Reference Include="MyLib">        
     <!-- // Removed HintPath, VS looks for references in $(OutDir) --> 
</Reference> 


The search order is specified in the file Microsoft.Common.targets
See: HintPath vs ReferencePath in Visual Studio

Community
  • 1
  • 1
Nils Lande
  • 860
  • 9
  • 14
0

Yes, but probably not natively inside VS2010. You can edit the .csproj file and use Condition attributes to create the references to Release or Debug.

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <HintPath>..\lib\Debug\MyLib.dll</HintPath>
</Reference>

or

<Reference Include="MyLib" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <HintPath>..\lib\Release\MyLib.dll</HintPath>
</Reference>
Otávio Décio
  • 73,752
  • 17
  • 161
  • 228