4

I am writing a visual studio add-in which preforms operations on javascript files after they have been added, removed, renamed and moved. The first 3 events are exposed by ProjectItemsEvents class, along he lines of:

this._applicationObject = (DTE2)application;
this._addInInstance = (AddIn)addInInst;

this.events = _applicationObject.Events as Events2;
this.projectItemsEvents = events.ProjectItemsEvents;
this.projectItemsEvents.ItemRenamed += ProjectItemRenamed;

However, there does not appear to be a way to detect when a file is moved, for instance between solution folders. Performing such an operation does even not appear to result in an ItemRemoved and ItemAdded event as I might expect.

Does anybody know how I should go about detecting the file moved event?

Will Holley
  • 1,745
  • 10
  • 11

2 Answers2

3

After digging some time on this I found a suitable way to detect file move actions.

You should first subscribe to item change events for projects inside a solution in your Package code by implementing IVsSolutionEvents, use IVsSolution.AdviseSolutionEvents, and listen to OnAfterOpenProject.

You should also implement IVsHierarchyEvents and subscribe to e.g. OnItemAdded and OnItemDeleted events by calling IVsHierarchy.AdviseHierarchyEvents. File moves can be detected by subsequent occurence of OnItemAdded and OnItemDeleted:

public class MyVsHierarchyEvents : IVsHierarchyEvents
{
   public int OnItemAdded( uint itemidParent, uint itemidSiblingPrev, uint itemidAdded )
   {
      object itemAddedExtObject;
      if ( m_hierarchy.GetProperty( itemidAdded, (int)__VSHPROPID.VSHPROPID_ExtObject, out itemAddedExtObject ) == VSConstants.S_OK )
      {
         var projectItem = itemAddedExtObject as ProjectItem;
         if ( projectItem != null )
         {
            // do something here ...
         }
      }
      return VSConstants.S_OK;
   }

   public int OnItemDeleted( uint itemid )
   {
      object itemExtObject;
      if ( m_hierarchy.GetProperty( itemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out itemExtObject ) == VSConstants.S_OK )
      {
         var projectItem = itemExtObject as ProjectItem;
         if (projectItem != null)
          {
             // do something here ...
          }
      }
      return ret;
   }
   // handle other events ...
}
public class MySolutionEvents : IVsSolutionEvents
{
   IVsHierarchyEvents m_myVsHierarchyEvents;
   uint m_cookie;
   public int OnAfterOpenProject( IVsHierarchy pHierarchy, int fAdded )
   {
       m_myVsHierarchyEvents = new MyVsHierarchyEvents();
       pHierarchy.AdviseHierarchyEvents( m_myVsHierarchyEvents, out m_cookie );
       // do other things here ...
       return VSConstants.S_OK;
   }
   // handle other events ...
}
public class MyPackage : Package
{
   IVsSolutionEvents m_vsSolutionEvents;
   IVsSolution m_vsSolution;
   uint m_SolutionEventsCookie;
   protected override void Initialize()
   {
      m_vsSolutionEvents = new MySolutionEvents();
      m_vsSolution = GetService( typeof( SVsSolution ) ) as IVsSolution;
      m_vsSolution.AdviseSolutionEvents( m_vsSolutionEvents, out m_SolutionEventsCookie );
   }
}

NOTE:

I also created a detector using FileSystemWatcher, as vc 74 suggested. However, it was somehow tricky to figure out suitable patterns of Create / Change / Delete events because different ones exist at least in in VS 2010 professional that occur after a file is moved inside a project. From my observation for the file becoming moved the create event is triggered first, followed by two change events, and finally a delete event. But, I also observed that sometimes there occurs another update event for the destination directory at different times.

Joerg Reinhardt
  • 358
  • 4
  • 9
1

You could use FileSystemWatcher to monitor the solution's directory yourself. When moving a file, it should fire a Deleted event followed by a Created event.

vc 74
  • 37,131
  • 7
  • 73
  • 89
  • This won't work completely here. A project item in Visual Studio doesn't necessarily have a real backing file. It could be an in-memory / designer generated item – JaredPar Feb 14 '12 at 08:37
  • @JaredPar The OP mentioned js files so I think these are physical files. – vc 74 Feb 14 '12 at 08:40