HintPath on a added reference in Visual Studio

浪子不回头ぞ 提交于 2019-11-28 05:46:20

Sorry, you can't use multiple HintPath's. Visual Studio/MSBuild takes only the last <HintPath> definition and will ignore any previous ones. Confirmed in VS2010 and VS2012.

Maxime Rouiller

This answer is no longer valid. As Sardaukar's comment says, Visual Studio always blindly uses the last HintPath. Alex's answer supports this.


Alright. I'm faster than Stackoverflow this time. I tried to add it and it seems to work fine.

So multiple HintPath IS possible.

When you have this:

<Reference Include="System.ComponentModel.Composition.Codeplex">
    <HintPath>..\..\..\MEF2_Preview2\bin\System.ComponentModel.Composition.Codeplex.dll</HintPath>
</Reference>

You can simply add more hint path like that:

<Reference Include="System.ComponentModel.Composition.Codeplex">
    <HintPath>..\..\..\MEF2_Preview2\bin\System.ComponentModel.Composition.Codeplex.dll</HintPath>
    <HintPath>D:\MEF\System.ComponentModel.Composition.Codeplex.dll</HintPath>
</Reference>

You can use environment variables for that. E.g.

<Reference Include="System.ComponentModel.Composition.Codeplex">
    <HintPath>$(PathToDLLs)\MEF2_Preview2\bin\System.ComponentModel.Composition.Codeplex.dll</HintPath>
</Reference>

Using Condition you can:

<Reference Include="TheAssembly">
    <HintPath Condition="Exists('..\My\Assembly\Path')">..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\My\Assembly\Path')">..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\My\Assembly\Path')">..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\My\Assembly\Path')">..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    etc...
</Reference>

The last HintPath where the Condition evaluates to true will be used.

Vladimir Shutow

Add the following to the bottom of your project file just after the commented out targets section:

<Target Name="BeforeResolveReferences">
  <CreateProperty Value="YOUR_FIRST_PATH;YOUR_SECOND_PATH;$(AssemblySearchPaths)">
    <Output TaskParameter="Value" PropertyName="AssemblySearchPaths" />
  </CreateProperty>
</Target>

Replacing YOUR_FIRST_PATH and YOUR_SECOND_PATH with your paths.

It's important this goes after the following line or your setting will be overwritten:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

With the $(AssemblySearchPaths) entry at the end of the string DLLs in your paths will override the normal resolution. If you move it to the beginning then the normal resolution is tried first and the additional paths are checked for any that weren't found. Normal resolution includes <HintPath> sections so there is no need to remove them if your paths come first.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!