0

I am making a program to search for a user defined string in multiple *.RTF files (about 1200 files). All of the files are saved in a directory on my computer. So far I can search through one file fairly easily using the code below, but when I tried running it on all the files, it took about 3 minutes to complete the search. Can any one help me with a better search algorithm?

lookup = this.textBox1.Text.ToString();
MatchCollection matches = Regex.Matches(richTextBox1.Text, lookup);

foreach (Match match in matches)
{
    richTextBox1.Select(match.Index, match.Length);
    richTextBox1.SelectionColor = System.Drawing.Color.Red;
}
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
Bob Ezuba
  • 510
  • 1
  • 5
  • 22
  • 1
    Use [String.indexOf](http://msdn.microsoft.com/en-us/library/7cct0x33.aspx) method instead of Regex, it will be faster – Iłya Bursov Oct 16 '13 at 00:32
  • 2
    And also, are you loading each RTF file into a GUI text box before you search it? That will take a lot of unnecessary time. – howrad Oct 16 '13 at 00:33
  • Thanks for the responses, so far i have been loading every file into the Rich Text Box as am using richTextBox1.LoadFile() to open the rtf files – Bob Ezuba Oct 16 '13 at 01:04
  • Sounds like you want to display a list of files matching a given search string. The code you posted above will find matches in a file and highlight them but immediately discard the result if this is done for multiple files. Much faster would be to search the raw RTF, ignore RTF markup, keep track of text locations. [This answer](http://stackoverflow.com/questions/10403678/converting-from-rich-text-format-to-plain-text-problems/10542417#10542417) might help, or you could use [IFilter](http://www.codeproject.com/Articles/13391/Using-IFilter-in-C) to process the files. – groverboy Oct 16 '13 at 03:34

0 Answers0