title: EnvDTE - Get the Find text results from 'FindResult 1" window in Visual Studio with C#
body: <p>According to <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/0d6a9da2-83f5-4195-a887-1173d7e8a459/unable-to-get-the-find-text-results-from-findresult-1-window?forum=vsx" rel="nofollow noreferrer">this post</a>, you can make Visual Studio find.</p>
  
  <p>I update the code of Asif Iqbal K from the article a bit to eliminate build error.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>So the question is: replace what code with the code </p>
  
  <pre><code>System.Threading.Thread.Sleep(5000);
  </code></pre>
  
  <p>So that the function FindInFiles() can get all the text of 'FindResult 1" window.</p>
  
  <p>Thanks for reading.</p>