3

According to this post, you can make Visual Studio find.

I update the code of Asif Iqbal K from the article a bit to eliminate build error.

public const string vsWindowKindFindResults1 = "{0F887920-C2B6-11D2-9375-0080C747D9A0}";
public string FindInFiles(string searchText)
{
    EnvDTE80.DTE2 dte;
    dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
    dte.MainWindow.Activate();
    EnvDTE.Find find = dte.Find;
    find.Action = EnvDTE.vsFindAction.vsFindActionFindAll;
    find.FindWhat = searchText;
    find.MatchWholeWord = false;
    find.ResultsLocation = EnvDTE.vsFindResultsLocation.vsFindResults1;
    find.Target = EnvDTE.vsFindTarget.vsFindTargetSolution;
    find.PatternSyntax = EnvDTE.vsFindPatternSyntax.vsFindPatternSyntaxRegExpr;
    find.SearchSubfolders = true;
    var x = dte.Find.FindWhat;
    EnvDTE.vsFindResult result = find.Execute();
    var findWindow = dte.Windows.Item(vsWindowKindFindResults1);
    string data = "";

    System.Threading.Thread.Sleep(5000);//Comment out this code to see the problem, this line of code is not the solution though.

    if (result == EnvDTE.vsFindResult.vsFindResultFound)
    {
        var selection = findWindow.Selection as EnvDTE.TextSelection;
        selection.SelectAll();
        data = selection.Text;
    }
    return data;
}

I see that the problem is the function return the string (string data) too early, so it can't get all the text from the result window.

So the code comes so close to get the find text. One remaining puzzle is to check if the find process complete, then get the text.

So the question is: replace what code with the code

System.Threading.Thread.Sleep(5000);

So that the function FindInFiles() can get all the text of 'FindResult 1" window.

Thanks for reading.

123iamking
  • 2,387
  • 4
  • 36
  • 56

1 Answers1

1

Here is the solution

    EnvDTE80.DTE2 s_dte;
    EnvDTE.FindEvents s_findEvents;
    public const string vsWindowKindFindResults1 = "{0F887920-C2B6-11D2-9375-0080C747D9A0}";

    public frmFindHelper()
    {
        InitializeComponent();

        s_dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
        s_dte.MainWindow.Activate();
        s_findEvents = s_dte.Events.FindEvents;
        s_findEvents.FindDone += new EnvDTE._dispFindEvents_FindDoneEventHandler(OnFindDone);
    }

    private void OnFindDone(EnvDTE.vsFindResult result, bool cancelled)
    {
        if (result == EnvDTE.vsFindResult.vsFindResultFound)
        {
            var findWindow = s_dte.Windows.Item(vsWindowKindFindResults1);
            string data = "";
            var selection = findWindow.Selection as EnvDTE.TextSelection;
            selection.SelectAll();
            data = selection.Text;
            MessageBox.Show("Done!");
        }
    }

    private void btnFind_Click(object sender, EventArgs e)
    {
        EnvDTE.Find find = s_dte.Find;
        find.Action = EnvDTE.vsFindAction.vsFindActionFindAll;
        find.FindWhat = txtSearch.Text;
        find.MatchWholeWord = false;
        find.ResultsLocation = EnvDTE.vsFindResultsLocation.vsFindResults1;
        find.Target = EnvDTE.vsFindTarget.vsFindTargetSolution;
        find.PatternSyntax = EnvDTE.vsFindPatternSyntax.vsFindPatternSyntaxRegExpr;
        find.SearchSubfolders = true;
        var x = s_dte.Find.FindWhat;
        EnvDTE.vsFindResult result = find.Execute();
    }

Thanks to Ed Dore from this post

123iamking
  • 2,387
  • 4
  • 36
  • 56