TextSearch (Release 1.0) - From Multiple Text Documents

TextSearch-1.0 reveals the track to enhance the search functionality of Text Documents belonging to the same path/folder.


TextSearch-1.0 allows users to search for a specific word from many text documents (File Format: *.txt) that belongs to the same directory, with no need to open individual files to search for a word or text. It has a simple GUI.

About the classes used:

  1. The FolderBrowserDialog class allows users to select a folder.
  2. The Directory class allows a user to enumerate the files available at a specified directory path.
  3. The StreamReader class provides a access for reading the data from a Stream such as a Text File.
  4. The Process class, to allow a user to start a specific process.

Namespace Required


Controls Used

The controls that are used are:

  1. TextBox Control (txtFolder, txtSearch)
  2. ListBox Control (lbFiles)
  3. Button Control (btnFolder, btnSearch, btnReset)

Here I present the code for finding a text file(s) with a specified word or text provided as a search string.

The Code

1. Variable Declarations:

String target = ""; // Target string to Search
String path = "";   // Path of Directory
string[] files;     // Store file name(s) from given path
bool search;        // Determine whether Target string present or NOT
string[] list;      // Store file name(s) which includes Target string

Listing 1

2. List All Text File(s) (File Format: *.txt)

void getFiles()
                  lbFiles.Items.Clear();    //Clears the Previous file list (if any)
                  path = txtFolder.Text;    //Current path of selected folder

              if (path != ""){

                  // Collects the name of files with extension of ".txt"
                  files = Directory.GetFiles(@path, "*.txt");

                  foreach (string item in files)  //Add File Name to ListBox

                  //Display Total No. of Files
                  lblTotal.Text = "Total Files: " + lbFiles.Items.Count.ToString(); }

Listing 2

3. After clicking the "Folder" button, assign the selected path from to txtFolder TextBox, & call the getFiles() method to list all files within that folder.

4. List All Text files which includes a string to be searched.

void Search()
            int j = 0;        // to set the index of "list" array
            target = txtSearch.Text;
            list = new string[lbFiles.Items.Count];
            for (int i = 0; i < lbFiles.Items.Count; i++)
                  search = false;

                  //Check whether Target String available within List of Text File(s)
                  if (!((new StreamReader(path + "\\"
                                    + lbFiles.Items[i]).ReadToEnd()).Contains(target)))
                  { search = true; }

                  list.SetValue(search.ToString(), j);
            //Delete File(s) from Search (if Search Not Found)
            for (int i = list.Length - 1; i >= 0; i--)
                  if (list.GetValue(i).ToString() != "False")

Listing 3

5. Call a Search() method to list the result text files at an OnClick Event of "Search" button.

6. Now execute the Application & see the intended results (Figure 1 & 2).

Intended Result

Figure 1.jpg

Figure 1: After selecting a folder

Figure 2.jpg

Figure 2: After specifying a string to search

Additional Functionality

  • TextSearch-1.0 allow to regain a list of initial text files as before search ("Reset Search").
  • The user can open a file, by double-clicking the file name.


The Program TextSearch-1.0 is written in Visual C#, which allows a user to search for text files which include a specified word or text.

Similar Articles