Summary:
We need to duplicate the behaviour of the Add Reference dialog, using DTE, when you add a specific DLL (it adds a Hint path entry to the reference in the CSProj file).
**Note: There is another related, but not duplicated, post from me here: https://stackoverflow.com/questions/6690655/visual-studio-2010-add-in-how-to-get-a-references-hint-path-property Please also read that one for more information about this issue. I have now added a decent bounty to get an answer to this and will happily spread up-votes over any decent answers :)*
The story so far:
I am converting a project reference to a direct DLL reference programmatically using DTE.
Assuming I have a simple solution with a Project2
(the parent project) which references a Project1
(the child project), I make the change like this:
project1Reference = FindProjectReference(project2.References, project1);
project1Reference.Remove();
Reference dllReference = project2.References.Add(project1DllPath);
where project1DllPath refers to the "c:\somewhere\Project1\Bin\Debug\Project1.dll"
file.
The problem I cannot yet solve is that the new reference is not to
"c:\somewhere\Project1\Bin\Debug\Project1.dll"
but instead points to
"c:\somewhere\Project2\Bin\Debug\Project1.dll"
(and the file is copied there).
If I add the DLL directly/manually using the Add Reference menu, it does not do this copying.
How do I add a DLL reference to an existing project's DLL without it taking a copy and referencing that instead?
I have tried adding dllReference.CopyLocal = false;
after the Add but aside from setting the flag it made no difference. There appear to be no options to modify the path after creation.
Update: I have also tried programmatically removing any Build dependency on Project1 from Project2, but that had no effect.
Below is the difference between the csproj files:
As a project:
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
<Project>{86B3E118-2CD1-49E7-A180-C1346EC223B9}</Project>
<Name>ClassLibrary1</Name>
</ProjectReference>
</ItemGroup>
As a DLL reference (path was lost completely):
<ItemGroup>
<Reference Include="ClassLibrary1, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
...
</ItemGroup>
As a manually referenced DLL:
<ItemGroup>
<Reference Include="ClassLibrary1, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\ClassLibrary1\bin\Debug\ClassLibrary1.dll</HintPath>
</Reference>
...
</ItemGroup>
It looks like being able to specify the hint path for the DLL reference is the key. How do you set a hint path on a DLL reference (assuming you only have a handle to the Reference property)?
More information (20 July 2011):
The suggestion from Muse VSExtensions below does not impact the DLLs in question, as a copy has already been made from the DLL's project BIN to the parent project's BIN folder. The parent project does not bother to make use of the reference path as it already has the child DLL in its output folder.
Also the Reference Paths
of a project are saved to the project.csproj.user file and not to the project.csproj file.