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.