Logo
HOWTO: Get an EnvDTE.Project object from its project unique name

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: June 2007   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

Introduction

Projects in Visual Studio have three names:

  • The name of the project in the Solution Explorer (EnvDTE.Project.Name property).
  • The full name of the project file (EnvDTE.Project.FullName property),

Since a solution could contain two projects with the same name saved in different folders, the name of a project is not unique, and can't be used to locate a project in a solution. To solve this, Visual Studio provides a third name property:

  • The unique name of a project (EnvDTE.Project.UniqueName property). If needed, the unique name of a project contains the folder where it is located.

The unique name of a project identifies it unambiguously. This article explains how to get the EnvDTE.Project object given its project unique name.

More Information

Given a project unique name, for example the Project parameter of the OnBuildProjConfigBegin event of the EnvDTE.BuildEvents class:

Public Event OnBuildProjConfigBegin(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String)

sometimes you need to get the EnvDTE.Project object that is being built. You can't use the EnvDTE.Solution.Projects collection to locate the EnvDTE.Project, since that collection only lists first-level projects, and a project can be nested inside a folder or another project. Although you could locate the EnvDTE.Project object iterating recursively that collection (as described in the article HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in), that approach would be overkill. The extensibility model offers the following method to retrieve the EnvDTE.Project of a project unique name:

EnvDTE.Solution.Item(ByVal projectUniqueName As String) As EnvDTE.Project.



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)