0

I need to create an extension to do some custom activity, on File Save Event in visual studio. How to achieve this using VS Package.

I am struck with below code, any advise?

    #region Package Members

    FileEventsListener listener = null;

    protected override void Initialize()
    {
        Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
        base.Initialize();

        listener = new FileEventsListener();

    }

    #endregion
}

public class FileEventsListener : IPersistFileFormat, IDisposable
{
    private IVsSolution solution;
    private uint solutionEventsCookie;
    private bool isDirty;
    private string fileName;
    private const uint MyFormat = 0;
    private const string MyExtension = ".edmx";

    public FileEventsListener()
    {
        //solution = Package.GetGlobalService(typeof(SPersistFileFormat)) as IVsSolution;

        //if (solution != null)
        //{
        //    solution.AdviseSolutionEvents(this, out solutionEventsCookie);
        //}
    }

    #region IDisposable Members

    public void Dispose()
    {

    }

    #endregion

    public int GetClassID(out Guid pClassID)
    {
        ErrorHandler.ThrowOnFailure(((IPersist)this).GetClassID(out pClassID));
        return VSConstants.S_OK;
    }

    public int GetCurFile(out string ppszFilename, out uint pnFormatIndex)
    {
        pnFormatIndex = MyFormat;
        ppszFilename = fileName;
        return VSConstants.S_OK;
    }

    public int GetFormatList(out string ppszFormatList)
    {
        char Endline = (char)'\n';
        string FormatList = string.Format(CultureInfo.InvariantCulture, "My Editor (*{0}){1}*{0}{1}{1}", MyExtension, Endline);
        ppszFormatList = FormatList;
        return VSConstants.S_OK;
    }

    public int InitNew(uint nFormatIndex)
    {
        if (nFormatIndex != MyFormat)
        {
            return VSConstants.E_INVALIDARG;
        }
        // until someone change the file, we can consider it not dirty as
        // the user would be annoyed if we prompt him to save an empty file
        isDirty = false;
        return VSConstants.S_OK;
    }

    public int IsDirty(out int pfIsDirty)
    {
        pfIsDirty = 0;

        return VSConstants.S_OK;
    }

    public int Load(string pszFilename, uint grfMode, int fReadOnly)
    {
        fileName = pszFilename;
        return VSConstants.S_OK;
    }

    public int Save(string pszFilename, int fRemember, uint nFormatIndex)
    {
        return VSConstants.S_OK;
    }

    public int SaveCompleted(string pszFilename)
    {
        return VSConstants.S_OK;
    }
}

}

Anand Kumar
  • 395
  • 2
  • 7
  • 18

1 Answers1

2

You can subscribe to the DTE.Events.DocumentEvents.DocumentSaved event.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66