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

By | January 3, 2013

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

[sourcecode language=”csharp”]
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);
}
}
[/sourcecode]

Step 4: Write following in click event of the buttons

[sourcecode language=”csharp”]
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();
}
}
[/sourcecode]

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

[sourcecode language=”csharp”]
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;
}
[/sourcecode]

Output:

Search Result

0 thoughts on “Search a folder recursively in a Windows Forms application using C#

  1. bjuraga

    Have you tested this when the root folder is for example @”C:\” ? It will crash on the recycle bin and some of the system folders like SystemVolumeInformation etc. At least if running under UAC. This is a very good article and i would suggest adding permissions awareness to it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *