7

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

but I can't seem to find the string or at least return the information. Any help would be greatly appreciated.

My current 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 != "" && 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() >= 0)
                              {
                                   strInputLine = sr.ReadLine();

                                   if (strInputLine.Contains(strSerial))
                                   {
                                        strOutput = Convert.ToString(strInputLine);
                                        lblOutput.Text = Convert.ToString(strOutput);
                                   }
                              }
                        }
                   }
              }
         }
    }
}
Rod
  • 14,529
  • 31
  • 118
  • 230
TimmRH
  • 142
  • 1
  • 1
  • 9

3 Answers3

12

You seem quite lost. Why are you using a dynamic when a string is all that you need? Your code has too many unnecessary variables and convertions. Here's a much simpler way to do it. I don't know what you want the label to have if there are many matching lines, here I'm only placing the first one there:

string dirScanner = @"\\mypath\";

if (string.IsNullOrWhiteSpace(txtSerialSearch.Text) || string.IsNullOrWhiteSpace(txtSID.Text))
    return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.txt");

foreach (string file in allFiles)
{
    string[] lines = File.ReadAllLines(file);
    string firstOccurrence = lines.FirstOrDefault(l => l.Contains(txtSerialSearch.Text));
    if (firstOccurrence != null)
    {
        lblOutput.Text = firstOccurrence;
        break;
    }
}
TimmRH
  • 142
  • 1
  • 1
  • 9
Andrew
  • 7,602
  • 2
  • 34
  • 42
3

I have implemented the same using Regular Expressions. You need to use namespace using System.Text.RegularExpressions;

 string strSerial = @"Microsoft";
            Regex match = new Regex(strSerial);
            string matchinglines = string.Empty;
            List<string> filenames = new List<string>(Directory.GetFiles(textBox1.Text));
            foreach(string filename in filenames)
            {
                //StreamReader strFile = new StreamReader(filename);
                string fileContent = File.ReadAllText(filename);
                if(match.IsMatch(fileContent))
                {
                    label1.Text = Regex.Match(fileContent, strSerial).ToString();
                    break;
                }
            }
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
  • 1
    What's the regular expression for? And where is it? You pattern is just a literal string. A RegEx gives no benefit if you are just doing a `Contains`. – Andrew Sep 30 '16 at 21:35
  • It is just different approach. I agree with you. In this case, regular expression does not bring additional value. Moreover, in this case, your solution is more performance compared to regular expressions. http://stackoverflow.com/questions/2962670/regex-ismatch-vs-string-contains#2962689 – Venkataraman R Sep 30 '16 at 22:55
  • 1
    Thank you, they both worked, it is nice to learn different approaches to this. – TimmRH Sep 30 '16 at 23:11
0

Use System.LINQ:

var list_of_files_that_match = Directory.EnumerateFiles(dir).Where(delegate (string t)
    {
        return System.IO.File.ReadAllText(t).Contains(your_text);
    }).ToList();

This worked for me. Quick and simple.

celloguy
  • 1
  • 2