1

I have a browse function, and i want something like this:

if (browsedFile.Text.Contains("Example")) //If the browsed file contains the text
{
   MessageBox.Show("Found");
}
else
{
   MessageBox.Show("Not Found");
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2944342
  • 105
  • 2
  • 2
  • 10

2 Answers2

1

There are many approaches to this problem, the simplest is probably the following:

using (OpenFileDialog open = new OpenFileDialog())
{
    if (open.ShowDialog() == DialogResult.OK)
    {
        if (File.ReadAllText(open.FileName).Contains("Example"))
        {
            MessageBox.Show("Found");
        }
    }
}

However you've mentioned larger files. If you're reading files with a gig or more you may want to look at the following approach instead:

using (OpenFileDialog open = new OpenFileDialog())
        {
            if (open.ShowDialog() == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(open.FileName))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Contains("Example"))
                        {
                            MessageBox.Show("Found");
                            break;
                        }
                    }
                }
            }
        }
Liath
  • 9,913
  • 9
  • 51
  • 81
  • 1
    if file size > 100 Mb and more it may be a bad idea... better to process predefined chunks of stream with per character search – Nogard Dec 12 '13 at 13:15
  • @Nogard I agree 100%, however from the OP I got the feeling he was looking for quick and simple rather than expandable. – Liath Dec 12 '13 at 13:18
  • @user2944342, this won't work with large files because it reads the entire file into memory. – Mike Perrenoud Dec 12 '13 at 13:19
  • I think we need to define large, there are indeed better alternatives if you're talking about big files including loading a line at a time. What file sizes are you looking at @user2944342 ? – Liath Dec 12 '13 at 13:20
1

Just for posterity, here is a solution that will work for larger files:

using (OpenFileDialog ofd = new OpenFileDialog())
{
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        foreach (var line in File.ReadLines(ofd.FileName))
        {
            if (!line.Contains(textToFind)) { continue; }

            // do something
        }
    }
}

The File.ReadLines method uses what's called deferred execution. In other words, it reads one line of the file into memory at a time and releases the previous when a new one is iterated. There are two advantages with this approach:

  1. You only read one line at a time.
  2. You may not have to read the entire file. If the text is found on line 2, only 2 lines are ever read from the file.

However, there is a caveat with this solution. If the text you're looking for contains a new line constant (i.e. you're searching for text across lines) this won't work because you don't have enough context.

It's likely that the solution provided by Liath is quite sufficient for your needs.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Ha - you go there before me! Out of interest does ReadLines read in line in turn rather than loading everything into memory? I wasn't 100% so went down the stream reader route. – Liath Dec 12 '13 at 13:27
  • 1
    @Liath, yes it will read one line and then issue a `yield`, thus when iterated again it reads the next line. Therefore, just one line is in memory at a time. – Mike Perrenoud Dec 12 '13 at 13:30
  • How about if i want the browse for file on button1 and find text on button2? – user2944342 Dec 12 '13 at 13:42
  • @user2944342, then you just have to save the file name in a class variable and instead of passing in the `ofd.FileName`, you'll use the variable. – Mike Perrenoud Dec 12 '13 at 13:46