7

I create a new solution and add some projects to it via the Solution2.AddFromTemplate. Now before I can build my solution successfully, I need to add a project reference from one of the projects to the other. I'm trying to navigate the VS automation object model, but cannot find how to do this.

I realize that I could just open the csproj as XML and change it on disk (as suggested here), but then I need to handle Visual Studio detecting the project file changing and prompting to reload it.

Anyone know how to do this or point me in the right direction?

Community
  • 1
  • 1
MvdD
  • 22,082
  • 8
  • 65
  • 93
  • 1
    Right click on the project -> Add reference -> Solution -> choose another project to add. Isn't this sufficient? – filipko May 10 '13 at 21:57
  • No, I'm doing this through the Visual Studio automation API. There's no clicking UI element as Visual Studio UI isn't even displayed. See also this link: http://msdn.microsoft.com/en-us/library/envdte%28v=vs.80%29.aspx – MvdD May 11 '13 at 18:24

1 Answers1

7

Found the answer, posting for future reference.

The trick is to cast the Object property of the EnvDTE.Project to VSProject and then call AddProject on its References property.

var targetProject = (VSProject) _project.Object;
targetProject.References.AddProject(sourceProject);
MvdD
  • 22,082
  • 8
  • 65
  • 93