Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > How to add a reference to a project programmatically?
Ask a questionAsk a question
 

AnswerHow to add a reference to a project programmatically?

  • Wednesday, November 04, 2009 8:03 PMMehdi58 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I have solution with 4 projects. I want to add a reference from one project to another one programmatically? does anybody know how to do that?

    thanks

Answers

  • Wednesday, November 04, 2009 10:01 PMAaron MartenMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Mehdi,

    You'll need to use the AddProject method on the References collection of a VSLangProj instance. Here's an example:

    private void AddProjectReference(EnvDTE.Project proj, EnvDTE.Project targetProject)
    {
        VSLangProj.VSProject vsProject = (VSLangProj.VSProject)proj.Object;
        vsProject.References.AddProject(targetProject);
    }
    

     


    http://blogs.msdn.com/aaronmar
    Thanks,
    Aaron

All Replies

  • Wednesday, November 04, 2009 10:01 PMAaron MartenMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Mehdi,

    You'll need to use the AddProject method on the References collection of a VSLangProj instance. Here's an example:

    private void AddProjectReference(EnvDTE.Project proj, EnvDTE.Project targetProject)
    {
        VSLangProj.VSProject vsProject = (VSLangProj.VSProject)proj.Object;
        vsProject.References.AddProject(targetProject);
    }
    

     


    http://blogs.msdn.com/aaronmar
    Thanks,
    Aaron
  • Sunday, November 22, 2009 3:42 PMMehdi58 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks a Lot Aaron
    That's completely right