Search for a File with Specific Text

Problem

You need to perform a search for a file that contains specific text.

Solution

Search through a file character-by-character using the FileStream.ReadByte method, and try to build up a matching string.

Discussion

Full-text searching is fairly easy to implement, although it can be time consuming, and it typically works best with text files. All you need to do is scan through a file, attempting to read each byte and convert it to a character. If you read a character that matches the requested text, you can then check to see if the next character matches, and so on.

The following FileTextSearcher class encapsulates the functionality required to perform a full-text search that works with any type of file.

Public ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.