因此,我尝试为 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/