HintPath on a added reference in Visual Studio

2019-01-17 03:27发布

问题:

I know that I can add a HintPath to an external DLLs to help Visual Studio/TFS find the dll when it builds.

What I was wondering is... is it possible to add multiple HintPath?

For example... developers have their DLLs for one place and we do a GetLatest of those DLLs at a different place on the server hence the need for multiple HintPath.

What do you think, world?

回答1:

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.



回答2:

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>


回答3:

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>


回答4:

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.



回答5:

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.