O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required