'Visual Studio SDK - Handling File Add, Remove, and Rename Events

I'm working on a Visual Studio extension that should listen for events when the user adds, removes, or renames files in the current solution.

The answer to this question notes that VS provides infrastructure for listening to document events like saving, opening and closing through the DocumentEvents interface. For example:

Dte.Events.DocumentEvents.DocumentSaved

Are there similar events that would allow me to listen to the user adding/removing/renaming documents?



Solution 1:[1]

You can subscribe to the EnvDTE.ProjectsEvents, EnvDTE.ProjectItemsEvents or IVsHierarchyEvents.

Solution 2:[2]

I know this is an old post by now, but for anyone else, who is searching for a fast solution. Take a look at the IVsTrackProjectDocuments2 class and it's matching IVsTrackProjectDocumentsEvents2 event interface.

You will receive notifications for all project items (Not solution items!), including Solution Items, which match the following actions:

  • Rename Directories
  • Rename Files

  • Add Directories

  • Add Files

  • Remove Directories

  • Remove Files

  • SccStatusChanged (I am guessing, that it will fire after a file's source-control state changed.)

These will contain an array of the changed items, their new state and the projects in which updates occurred. Additionally you will get a VS*FLAGS array, which contains more information about the current operation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakub Bielawa
Solution 2 Twenty