如何:自动进行文本搜索和替换

Visual Studio 2013 中已弃用 Visual Studio 的外接程序。 你应该升级外接程序到 VS 的扩展包。 有关升级的更多信息,请参见 。常见问题:将外接程序转换为 VSPackage 扩展

Visual Studio 使您能够在集成开发环境 (IDE) 中打开并在系统上的文件中包含的文档中搜索和替换文本。 完成此操作的主要方法是使用 Find 对象的 FindReplaceExecute 方法。 TextSelectionEditPoint 对象也提供 FindPattern 方法。 有关更多信息,请参见如何:控制代码编辑器 (Visual Basic)中的 FindPattern 方法。

备注

由于 FindPattern 方法搜索所有文本(包括隐藏文本),因此 [vsFindOptions] 枚举中的 vsFindOptionsMatchInHiddenTex[t] 常数值不适用于此方法。

EnvDTE80 命名空间中 Find 的版本名为 Find2。 它与 Find 对象相同,但它还提供一个名为 WaitForFindToComplete 的新属性。 当此布尔属性设置为 True 时,直到搜索完所有选定的文档后查找操作才会结束。

例如,如果要在 100 个文档中搜索某个单词,除非使用 WaitForFindToComplete 属性或者处理 FindDone 事件,否则您可能会接收到不完整的结果。 两种方法都可行,但设置 WaitForFindToComplete 属性是更加快捷、方便的方法,可以确保在显示搜索结果之前搜索完所有的文档。

备注

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关详细信息,请参阅在 Visual Studio 中自定义开发设置

示例

下面的示例说明如何引用和使用查找自动化模型的各种成员。 本示例创建带有一些文本的文本文档,然后使用不同方法搜索和替换文本。 若要运行此示例,请使用下面的代码替换简单外接程序中的 OnConnection 方法。 若要运行本示例的不同部分,请取消对适当代码的注释。 在运行此代码之前,请确保已将 EnvDTE 程序集引用的“嵌入互操作类型”属性设置为 False。

Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    searchReplace(_applicationObject)
End Sub

Public Sub searchReplace(ByVal dte As DTE2)
    Dim findWin As Find2
    Dim doc As Document
    Dim textDoc As TextDocument
    Dim textSel As TextSelection
    Dim iCtr As Integer

    ' Create a new text file.
    dte.ItemOperations.NewFile("General\Text File")

    ' Set up references for the text document, Find object, and
    ' TextSelection object.
    doc = dte.ActiveDocument
    textDoc = CType(doc.Object("TextDocument"), TextDocument)
    textSel = textDoc.Selection
    findWin = CType(dte.Find, Find2)
    ' Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = True

    ' Insert ten lines of text.
    For iCtr = 1 To 10
        textDoc.Selection.Text = "This is a test" & vbCr
    Next iCtr
    textDoc.Selection.Text = "This is a different word"

    ' Uses FindReplace to find all occurrences of the word, test, in 
    ' the document.
    MsgBox("Now changing all occurrences of 'test' to 'replacement'.")
    findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", _
      vsFindOptions.vsFindOptionsMatchCase, "replacement", _
      vsFindTarget.vsFindTargetCurrentDocument, , , _
      vsFindResultsLocation.vsFindResultsNone)

    ' Uses Find2.Execute to find the word, different, in the document.
    ' findWin.FindWhat = "different"
    ' findWin.MatchCase = True
    ' findWin.Execute()

    ' Uses Find2.Execute to replace all occurrences of the word, Test, 
    ' with the word, replacement.
    ' findWin.FindWhat = "test"
    ' findWin.ReplaceWith = "replacement"
    ' findWin.Action = vsFindAction.vsFindActionReplaceAll
    ' findWin.Execute()
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    searchReplace(_applicationObject);
}

public void searchReplace(DTE2 dte)
{
    Find2 findWin;
    Document doc;
    TextDocument textDoc;
    TextSelection textSel;
    int iCtr;

    // Create a new text file.
    dte.ItemOperations.NewFile("General\\Text File"
      ,"New file",Constants.vsViewKindTextView);

    // Set up references for the text document, Find object, and
    // TextSelection object.
    doc = dte.ActiveDocument;
    textDoc = (TextDocument) doc.Object("TextDocument");
    textSel = textDoc.Selection;
    findWin = (Find2) dte.Find;
    // Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = true;

    // Insert ten lines of text.
    for(iCtr=1; iCtr<=10; iCtr++)
    {
        textDoc.Selection.Text = "This is a test"+Environment.NewLine;
    }
    textDoc.Selection.Text = "This is a different word";

    // Uses FindReplace to find all occurrences of the word, test, in 
    // the document.
   System.Windows.Forms.MessageBox.Show(
     "Now changing all occurrences of 'test' to 'replacement'.");
   findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", 
     (int) vsFindOptions.vsFindOptionsFromStart, "replacement", 
     vsFindTarget.vsFindTargetCurrentDocument, "", 
     "",vsFindResultsLocation.vsFindResultsNone);

   // Uses Find2.Execute to find the word, different, in the document.
   // findWin.FindWhat = "different"
   // findWin.MatchCase = True
   // findWin.Execute()

   // Uses Find2.Execute to replace all occurrences of the word, Test, 
   // with the word, replacement.
   // findWin.FindWhat = "test"
   // findWin.ReplaceWith = "replacement"
   // findWin.Action = vsFindAction.vsFindActionReplaceAll
   // findWin.Execute()
}

请参见

任务

如何:编译和运行自动化对象模型代码示例

如何:控制代码编辑器 (Visual Basic)

如何:创建外接程序

演练:创建向导

概念

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考