【问题标题】:C#: Selecting currently active fileC#:选择当前活动的文件
【发布时间】:2013-07-03 07:51:07
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    您需要先获得对 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);
    

    有关 DTE2 接口的更多详细信息,请参阅此MSDN Page

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2013-02-18
      • 2021-10-20
      • 2021-11-05
      • 2016-06-04
      • 1970-01-01
      • 2015-02-03
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多