In VisualStudio DTE, how to get the contents of the ActiveDocument?

我与影子孤独终老i 提交于 2019-12-08 18:56:44

问题


I'm scripting inside VisualStudio and am trying to get the contents of the currently ActiveDocument.

This is my current solution:

var visualStudio = new API_VisualStudio_2010();

var vsDTE = visualStudio.VsAddIn.VS_Dte;

var document = (Document)vsDTE.ActiveDocument;
var textDocument = (TextDocument)document.Object("TextDocument");

var editPoint = textDocument.StartPoint.CreateEditPoint();
var text = editPoint.GetText(textDocument.EndPoint.CreateEditPoint());

panel.clear().add_SourceCodeViewer()
     .set_Text(text,  document.FullName.extension());

Is this the best way?

I got the solution from: Because ActiveDocument.Text() Would Be Too Easy...


回答1:


This is working for me

protected DTE2 dte;
dte2 = (EnvDTE80.DTE2)GetService(typeof(EnvDTE.DTE));

public string GetCurrentTextFile(){

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

  return s;            
}



回答2:


Can you give this a try?

Dim objSelection As TextSelection = DTE.ActiveDocument.Selection


来源:https://stackoverflow.com/questions/10606274/in-visualstudio-dte-how-to-get-the-contents-of-the-activedocument

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