在 VisualStudio DTE 中,如何获取 ActiveDocument 的内容? [英] In VisualStudio DTE, how to get the contents of the ActiveDocument?

查看:41
本文介绍了在 VisualStudio DTE 中,如何获取 ActiveDocument 的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 VisualStudio 中编写脚本,并试图获取当前 ActiveDocument 的内容.

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());

这是最好的方法吗?

我从以下位置得到了解决方案:因为 ActiveDocument.Text()太容易了...

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

推荐答案

这对我有用

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;            
}

这篇关于在 VisualStudio DTE 中,如何获取 ActiveDocument 的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆