Logo
HOWTO: Getting Project and ProjectItem events from a Visual Studio .NET add-in.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: September 2005   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

This article describes how to get Project and ProjectItem events (ItemAdded, ItemRemoved, ItemRenamed) from a Visual Studio .NET add-in.

More Information

The extensibility model of Visual Studio .NET provides the EnvDTE.ProjectEvents and EnvDTE.ProjectItemsEvents classes with the events ItemAdded, ItemRemoved and ItemRenamed, but the class EnvDTE.Events returned by the property EnvDTE.DTE.Events does not provide a direct way to get an instance of that class. The reason is that those events are language specific (C#, VB.NET, etc.), and the package installed for each language must provide those events through automation. The way to get an instance of the EnvDTE.ProjectEvents and EnvDTE.ProjectItemsEvents class is through the EnvDTE.Events.GetObject method, which receives the "name" of the events.

Some common names for the Project events are:

  • For Visual Basic .NET projects: "VBProjectsEvents"
  • For C# projects: "CSharpProjectsEvents"
  • For Visual J# projects: "VJSharpProjectsEvents"
  • For Visual Basic .NET smart device projects: "eVBProjectsEvents"
  • For C# smart device projects: "eCSharpProjectsEvents"

Some common names for the ProjectItem events are:

  • For Visual Basic .NET projects: "VBProjectItemsEvents"
  • For C# projects: "CSharpProjectItemsEvents"
  • For Visual J# projects: "VJSharpProjectItemsEvents"
  • For Visual Basic .NET smart device projects: "eVBProjectItemsEvents"
  • For C# smart device projects: "eCSharpProjectItemsEvents"
  • For VS 2005 (or higher) Web Site projects: "WebSiteItemsEvents"

Note: Visual Studio .NET throws exceptions trying to retrieve events for smart device projects (both C# and VB.NET) although the names are correct. This seems to be a bug.

To guess the names of other Project or ProjectItem events (or other automation events in general), you can do the following:

  • Run the regedit.exe tool.
  • Navigate to the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<version>\Packages

where <version> is:

7.0 for Visual Studio .NET 2002
7.1 for Visual Studio .NET 2003
8.0 for Visual Studio 2005
9.0 for Visual Studio 2008
10.0 for Visual Studio 2010
11.0 for Visual Studio 2012

  • Search for the string "AutomationEvents". Some packages have a registry key with this name and provide the names of several automation events, including ProjectsEvents and ProjectItemsEvents.

The following add-in sample (VB.NET) shows how to show a message box each time that a VB.NET or C# file is added to a project:

Public Class Connect
   Implements Extensibility.IDTExtensibility2
 
   Private m_objDTE As EnvDTE.DTE
   Private WithEvents m_objVBProjectItemsEvents As ProjectItemsEvents
   Private WithEvents m_objCSharpProjectItemsEvents As ProjectItemsEvents
 
   Public Sub OnConnection(ByVal application As Object, _
      ByVal connectMode As Extensibility.ext_ConnectMode, _
      ByVal addInInst As Object, ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnConnection
 
      Try
 
         m_objDTE = CType(application, EnvDTE.DTE)
         m_objVBProjectItemsEvents = CType( _
            m_objDTE.Events.GetObject("VBProjectItemsEvents"), ProjectItemsEvents)
         m_objCSharpProjectItemsEvents = CType( _
            m_objDTE.Events.GetObject("CSharpProjectItemsEvents"), ProjectItemsEvents)
 
      Catch objException As Exception
 
         MessageBox.Show(objException.ToString)

      End Try
 
   End Sub
  
   Private Sub ProjectItemsEvents_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _
      Handles m_objVBProjectItemsEvents.ItemAdded, m_objCSharpProjectItemsEvents.ItemAdded
 
      MessageBox.Show(ProjectItem.Name & " added to the project " & _
         ProjectItem.ContainingProject.Name)
 
   End Sub
 
   Public Sub OnBeginShutdown(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnBeginShutdown
   End Sub
 
   Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
   End Sub
 
   Public Sub OnStartupComplete(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnStartupComplete
   End Sub
 
   Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
      ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnDisconnection
   End Sub
 
End Class

The extensibility model of Visual Studio 2005 allows to use the same approach seen so far for Visual Studio .NET 2002/2003, but it also provides a new EnvDTE80.Events2 class which has the ProjectsEvents and ProjectItemsEvents properties to directly return the events, which fire for any .NET language.

The following add-in sample (VB.NET 2005) shows how to show a message box each time that a file (any language) is added to a project:

Private m_objDTE As EnvDTE.DTE
Private WithEvents m_objProjectItemsEvents As ProjectItemsEvents

Public Sub OnConnection(ByVal application As Object, _
      ByVal connectMode As Extensibility.ext_ConnectMode, _
      ByVal addInInst As Object, ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnConnection

   Dim objEvents2 As EnvDTE80.Events2

   Try

      m_objDTE = CType(application, EnvDTE.DTE)

      objEvents2 = CType(m_objDTE.Events, EnvDTE80.Events2)

      m_objProjectItemsEvents = objEvents2.ProjectItemsEvents

   Catch objException As Exception

      MessageBox.Show(objException.ToString)

   End Try

End Sub

Private Sub ProjectItemsEvents_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _
   Handles m_objProjectItemsEvents.ItemAdded

   MessageBox.Show(ProjectItem.Name & " added to the project " & _
      ProjectItem.ContainingProject.Name)

End Sub 


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