Quantcast
Channel: DEEPAK SHARMA » RunWorkerCompleted event in Windows Forms
Viewing all articles
Browse latest Browse all 2

Search a folder recursively in a Windows Forms application using C#

$
0
0

Download source code

Introduction

In this post I will explain how to search a folder recursively for some text and list all the files in the folder and its sub folders containing this text in a Windows Forms application using C#

Description

Step 1: Create a new Windows Forms application

New Project_2013-01-02_20-59-07

Step 2: Arrange controls on the Form1 as following and drag a BackgroundWorker on it

Search Directory_2013-01-02_21-22-59

Step 3: Write a function to search a given folder recursively

public void SearchDirectories(string Path, string SearchText)
{
     foreach (string FileName in Directory.GetFiles(Path))
     {
          FileInfo fi = new FileInfo(FileName);
          if (fi.Extension.Equals(".txt"))
          {
               StreamReader sr = new StreamReader(FileName);
               string FileContents = sr.ReadToEnd().ToLower();
               SearchText = SearchText.ToLower();
               if (fi.Name.ToLower().Contains(SearchText) || FileContents.Contains(SearchText))
               {
                    backgroundWorker1.ReportProgress(0, FileName);
               }
          }
     }
     foreach (string Folder in Directory.GetDirectories(Path))
     {
          SearchDirectories(Folder, SearchText);
     }
}

Step 4: Write following in click event of the buttons

private void btnSelectLocation_Click(object sender, EventArgs e)
{
     FolderBrowserDialog FolderBrowser = new FolderBrowserDialog();
     if (FolderBrowser.ShowDialog() == DialogResult.OK)
     {
          txtDirectory.Text = FolderBrowser.SelectedPath;
          txtSearchText.Focus();
     }
}

private void btnSearch_Click(object sender, EventArgs e)
{
     if (txtDirectory.Text.Length > 0)
     {
          lbSearchResult.Items.Clear();
          i = 0;
          lblTotalFiles.Text = i.ToString();
          btnSearch.Enabled = false;
          backgroundWorker1.RunWorkerAsync();
     }
     else
     {
          MessageBox.Show("Please select a folder!");
          txtDirectory.Focus();
     }
}

Step 5: Write following in the DoWork, ProgressChanged and RunWorkerCompleted events of the BackgroundWorker. Before that set its WorkerReportsProgress property to true

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
     SearchDirectories(txtDirectory.Text, txtSearchText.Text);
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     i++;
     lblTotalFiles.Text = i.ToString();
     lbSearchResult.Items.Add(e.UserState.ToString());
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     MessageBox.Show("Search completed!");
     btnSearch.Enabled = true;
}

Output:

Search Result


Filed under: .Net, WinForm

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images