Document Saved Event


How do I subscribe to the DocumentSaved event, so I can run my code whenever a document is saved?

I tried hooking into the standard Visual Studio events using:

var dte = Shell.Instance.GetComponent<DTE>();
dte.Events.DocumentEvents.DocumentSaved += this.DocumentEventsOnDocumentSaved;

This code works, if the project is set to .NET 3.5.

However the DocumentSaved event is never raised. I assume this is either a bug in Visual Studio or Resharper traps it.

Is there a similar event in Resharper?

1 comment

ReSharper also has a document saved event, which lives on the DocumentManagerOperations class. You can inject this into your plugin class, as long as your class is marked with the [SolutionComponent] attribute. Something like this:

public class MyPlugin
    public MyPlugin(DocumentManagerOperations ops)
        ops.AfterDocumentSaved += ops_AfterDocumentSaved;

    void ops_AfterDocumentSaved(object sender, DocumentSavedEventArgs e)
        // ...

You will get notified once the document has been saved, and the args passes in an instance of the Document class so that you examine the document. Note that there is also a BeforeDocumentSaved event, but this is marked as obsolete, so shouldn't be used.



Please sign in to leave a comment.