How to intercept the project save event in vssdk?

1

I'm currently developing an extension of visual studio 2017 where I need to know when any design changes are persisted.

Ex: When I add a new reference in the project (I know there are events for when the reference is added / changed / removed but did not meet my need), the project is marked as pending to be saved. I need to intercept when it is saved (best if it is before saving)

I tried the Dte.Events.DocumentEvents.DocumentSaved event, but it is not triggered in the project save; DTE.Events.SolutionEvents and DTE.Events.SolutionItemEvents do not have any event of the type that I need

Is this possible?

    
asked by anonymous 20.09.2017 / 14:53

2 answers

0

What worked for me as a glove was to implement the IVsRunningDocTableEvents3 interface by overriding the OnBeforeSave method.

That way I knew exactly when the project was about to be saved and to perform the actions I needed.

Eg:

uint cookie;
var runningDocumentTable = (IVsRunningDocumentTable)GetGlobalService(typeof(SVsRunningDocumentTable));

runningDocumentTable.AdviseRunningDocTableEvents(new RunningDocTableEventsHandler(), out cookie);
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace YourProject
{
    internal class RunningDocTableEventsHandler : IVsRunningDocTableEvents3
    {

        #region Methods

        public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterSave(uint docCookie)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeSave(uint docCookie)
        {
            /////// MY CODE ////////
            return VSConstants.S_OK;
        }

        #endregion Methods
    }
}
    
22.09.2017 / 19:36
0