C#:选择当前事件文件

标签 c# visual-studio-2012 add-in

因此,我尝试为 Visual Studio 2012 创建一个加载项,但遇到了困难。所以这就是我被困的地方。

假设我在 Visual Studio 中处理一个文件。假设它被称为 Default.aspx 。我的加载项是这样工作的:当您按下工具栏上的按钮时,它会将文件中的所有内容复制到一个字符串变量中,并用它做一些事情。

那么我可以使用哪个函数来“选择”当前打开的文件呢?我可以在 visual studio 中打开 4-5 个选项卡,但我只想选择当前正在处理的文件,在此上下文中将是 Default.aspx。有办法做到这一点吗?

最佳答案

您需要先获得对 IDE 的引用。

DTE2 IDE = (DTE2)GetService(typeof(DTE)));

从 DTE2 引用您可以访问事件文档

Document activeDoc = IDE.ActiveDocument;

要从文档对象中获取文本,您需要执行以下操作:

// Get a reference to the text document
TextDocument textDoc = activeDoc.Object("TextDocument");

// Create a reference point at the start of the document.
EnvDTE.EditPoint startPoint = textDoc.StartPoint.CreateEditPoint;

// Get all the text from our starting point to the end of the document.
string allText = startPoint.GetText(textDoc.EndPoint);

查看此 MSDN Page有关 DTE2 接口(interface)的更多详细信息。

关于C#:选择当前事件文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442513/

相关文章:

c# - 将 MVC DropDownlistFor() 绑定(bind)到模型的导航属性

c# - 无法在 Visual Studio 2012 中安装任何 NuGet

c++ - 立即执行 .then

visual-studio - 无法安装 Visual Studio 11 预览版

office365 - 具有 Office 加载项 AppDomains 的子域

c# - 使用仅包含静态方法而不包含变量的 C# 类时会出现并发问题吗?

c# - DataReader 返回 DBNULL

c# - 使用委托(delegate)执行任何方法并重试 1 次的通用方法

c# - 更改 Outlook 邮件项目选择 C#

c# - powerpoint c# 加载项形状分组问题