방법: 텍스트 찾기 및 바꾸기 자동화

Visual Studio에서는 IDE(통합 개발 환경)에 열려 있는 문서의 텍스트뿐 아니라 시스템의 파일에 포함된 텍스트도 검색하고 바꿀 수 있습니다.이 작업을 수행하는 기본적인 방법은 Find 개체의 FindReplaceExecute 메서드를 사용하는 것입니다.TextSelectionEditPoint 개체에는 FindPattern 메서드도 있습니다.자세한 내용은 방법: 코드 편집기 제어(Visual Basic)에서 FindPattern 메서드를 참조하십시오.

[!참고]

[vsFindOptions] 열거형의 vsFindOptionsMatchInHiddenTex[t] 상수 값은 숨겨진 텍스트까지 포함하여 모든 텍스트를 검색하므로 FindPattern 메서드에 적용되지 않습니다.

EnvDTE80 네임스페이스에 있는 버전의 FindFind2라는 이름을 가지고 있습니다.이 개체는 WaitForFindToComplete라는 새로운 속성을 제공한다는 점을 제외하고 Find 개체와 동일합니다.이 부울 속성을 True로 설정하면 선택한 모든 문서에 대한 검색이 끝나야 찾기 작업이 완료됩니다.

예를 들어, 100개의 문서에서 특정 단어를 검색하는 경우 완전한 검색 결과를 얻으려면 WaitForFindToComplete 속성을 사용하거나 FindDone 이벤트를 처리해야 할 수도 있습니다.두 방법 중 어느 것이나 사용할 수 있지만, 모든 문서에 대한 검색을 완료한 다음에 검색 결과를 표시하게 하려면 WaitForFindToComplete 속성을 설정하는 것이 더 간편하고 빠릅니다.

[!참고]

표시되는 대화 상자와 메뉴 명령은 활성 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다.이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다.설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다.자세한 내용은 Visual Studio 설정을 참조하십시오.

예제

다음의 예제에서는 찾기 자동화 모델의 다양한 멤버를 참조하고 사용하는 방법을 보여 줍니다.이 예제에서는 약간의 텍스트가 포함된 텍스트 문서를 만든 다음 서로 다른 메서드를 사용하여 텍스트를 검색하고 바꿉니다.이 예제를 실행하려면 간단한 추가 기능에 있는 OnConnection 메서드를 아래의 코드로 바꿉니다.이 예제의 다른 부분을 실행하려면 해당 코드의 주석 처리를 제거합니다.이 코드를 실행하기 전에 EnvDTE 어셈블리 참조의 "Interop 형식 포함" 속성이 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)

방법: 추가 기능 만들기

연습: 마법사 만들기

개념

자동화 개체 모델 차트

기타 리소스

환경 창 만들기 및 제어

추가 기능 및 마법사 만들기

자동화 및 확장성 참조