Gaston Milano's WebLog

About something...

News

General

Usability

VS Automation

VSIP

How to add references by automation

It's easy but I always forget the steps. I have to look in my code in order to remember. So here the steps:

1. Add a reference to VSLangProj. If you are writing a macro you don't need this step.
2. Add the using clause (C#)
            using VSLangProj; 

 3.    And use this function

  public bool AddReference(EnvDTE.Project project, string reference)
  {
      VSProject proj = m_Project.Object as VSProject;
      System.Diagnostics.Debug.Assert(proj != null); // This project is not a VSProject
      if (proj == null)
          return false;
      try
      {
          proj.References.Add(reference);
      }
      catch (Exception ex)
      {
          string message = String.Format("Could not add {0}. \n Exception: {1}", reference, ex.Message);
          System.Diagnostics.Trace.WriteLine(message);
          return false;
      }

      return true;
  }

In order to add a reference to a project you have to use proj.References.AddProject insted proj.References.Add.

If you wanna add a reference to an assembly that is in the GAC then pass just the dll name insted of the full path.


 

Posted: Sep 23 2004, 01:34 PM by GMilano | with 7 comment(s)
Filed under:

Comments

TrackBack said:

# May 18, 2005 3:42 PM

nishtha.bhatnagar said:

hi,

I want to add references in a VS 2008 project through code. I've included VSLangProj and ENVDTE. But not finding any options.

Any help...

# November 19, 2008 2:02 AM

Semil said:

<a href= spiritez.com ></a>

# December 5, 2008 6:03 PM

ellaelax-rz said:

<a href= membres.lycos.fr/dertull >zx10r graphics</a>

# December 26, 2008 11:13 AM

nick_lac4tm said:

# December 26, 2008 10:57 PM

Sam said:

I want to be able to add a reference even if the referenced assembly I'm adding cannot be resolved.  I know this may seem unusual, however for my purposes it fits my needs.

My problem really boils down to, I'd be able to construct a project correctly despite assemblies being missing.  Those should be able to be added later (imo).

# December 30, 2008 6:31 PM

jonmikeli@exakis.com said:

Hi,

I'm using automation to configure references between a set of automatically created projects.

But when I execute the code above nothing happens: no error, no exception and no reference is added.

I've checked that the 'References' property really contains the added references but when I open the project under VS, no reference has been added.

Any idea about the source of the problem?

Many thanks.

# July 8, 2009 10:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)