Run script when finished saving file - Visual Studio Extensibility

荒凉一梦 提交于 2019-12-31 06:23:20

问题


Can someone give me some sample code for Visual Studio Extensibility where I can grab the text from a document, when the Save event ends, and run a script in C # with that text (example, trigger a Web service for certain file extensions). It could also be a new button (for example, save in the web service).


回答1:


You can subscribe to the DocumentSaved event:

    events = DTE.Events;
    documentEvents = events.DocumentEvents;
    documentEvents.DocumentSaved += OnDocumentSaved;

In the OnDocumentSaved handler with EnvDTE.Document you can get the document path as doc.FullName.

To get text from EnvDTE.Document:

  TextDocument td = (TextDocument)(doc.Object("TextDocument"));
  var p = td.StartPoint.CreateEditPoint();
  string s = p.GetText(td.EndPoint);

See In VisualStudio DTE, how to get the contents of the ActiveDocument? and https://vlasovstudio.com/visual-commander/extensions.html for complete samples.



来源:https://stackoverflow.com/questions/49268744/run-script-when-finished-saving-file-visual-studio-extensibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!