title: C# search all files in a directory that contain a string, then return that string
body: <p>Using user input into a textbox, I want to search for which file in the directory contains that text. I would then like to parse out the information </p>
  
  <p>but I can't seem to find the string or at least return the information. Any help would be greatly appreciated. </p>
  
  <p>My current code:</p>
  
  <pre><code>private void btnSearchSerial_Click(object sender, EventArgs e)
  {
      dynamic dirScanner = @"\\mypath\";
      string strSerial;
      string strSID;
      string strInputLine;
      string strOutput;
  
      strSerial = Convert.ToString(txtSerialSearch);
      strSID = Convert.ToString(txtSID);
  
      if (txtSerialSearch.Text != "" &amp;&amp; txtSID.Text != "")
      {
           try
           {                    
                string[] allFiles = Directory.GetFiles(dirScanner);
  
                foreach (string file in allFiles)
                {
                     if (file.EndsWith(".txt"))                            
                     {  
                          using (StreamReader sr = new StreamReader(file))
                          {
                                while (sr.Peek() &gt;= 0)
                                {
                                     strInputLine = sr.ReadLine();
  
                                     if (strInputLine.Contains(strSerial))
                                     {
                                          strOutput = Convert.ToString(strInputLine);
                                          lblOutput.Text = Convert.ToString(strOutput);
                                     }
                                }
                          }
                     }
                }
           }
      }
  }
  </code></pre>