HOW TO:自動化文字檢索和取代
Visual Studio 提供了搜尋和取代文字的功能,可以讓您在整合式開發環境 (IDE) 中搜尋和取代開啟文件中的文字,也可以在系統中搜尋和取代檔案中所包含的文字。搜尋和取代的主要方式是使用 Find 物件的 FindReplace 和 Execute 方法。TextSelection 和 EditPoint 物件也提供了 FindPattern 方法。如需詳細資訊,請參閱 HOW TO:控制程式碼編輯器 (Visual Basic) 中的 FindPattern 方法。
注意事項 |
---|
[vsFindOptions] 列舉中的 vsFindOptionsMatchInHiddenTex[t] 常數值會搜尋所有文字 (包括隱藏的文字),因此並不適用於 FindPattern 方法。 |
Find 物件在 EnvDTE80 命名空間中的版本命名為 Find2。Find2 和 Find 物件相同,不過 Find2 還提供了一個名為 WaitForFindToComplete 的新屬性。如果將這個布林 (Boolean) 屬性設定為 True,在搜尋完所有選取的文件之前,尋找作業不會結束。
例如,如果您要在 100 份文件中搜尋某個單字,除非是使用 WaitForFindToComplete 屬性或是處理 FindDone 事件,否則得到的搜尋結果並不完整。這兩種方法都可以使用,不過設定 WaitForFindToComplete 屬性比較簡便,而且可以確保在所有文件都搜尋完之後才會顯示搜尋結果。
注意事項 |
---|
根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
範例
在下列範例中,會示範如何參考及使用 Find Automation 模型中的各個成員。這個範例會建立包含某些文字的文字文件,然後使用不同的方法搜尋和取代文字。若要執行這個範例,請以下列程式碼取代範例增益集 (Add-In) 中的 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()
}
請參閱
工作
HOW TO:編譯和執行 Automation 物件模型程式碼範例
HOW TO:控制程式碼編輯器 (Visual Basic)