Click here to Skip to main content
Click here to Skip to main content

File Searcher in C#

, 29 Sep 2012
Rate this:
Please Sign up or sign in to vote.
A freeware file searcher in C#

Introduction

On a Windows Vista PC, I tried to search my hard disk for a file that contains a certain string. This was not possible using Windows Explorer. So, I decided to write my own file searcher. And, here it is...

What It Does

You must enter a search directory, so the program knows where to search for files and directories. If you check the "Include subdirectories" checkbox, the program will recursively search in all subdirectories of the search directory. The filename specified can be something like "*.wav;*.mp3;Christma??ree.*". The program will list all files and directories matching these filenames.

You can also use some restrictions to restrict the found items. Each restriction must be activated by checking the appropriate checkbox, and the necessary parameter for this restriction can be entered to the right of the checkbox.

  • "Files newer than" will only list the items that have a LastWriteTime greater than the parameter.
  • "Files older than" will only list the items that have a LastWriteTime less than the parameter.
  • "Files containing the string" will only list the items that contain the string parameter you entered.
  • The program will convert this string parameter into a byte sequence, using either ASCII or Unicode encoding (depending on what you select), and then search each file for an occurrence of this byte sequence.

Click the "Start" button to start the search. The found items will be listed below. If the search takes too much time and you want to cancel it, you can click the "Stop" button to stop the search.

If you double-click an item that represents a file, the program will open this file within the application that is associated with the file's extension.

If you right-click an item and select "Open Containing Folder", the program will open the folder containing the file or directory in Windows Explorer.

If you want to write the search results to a text file, enter a delimeter that shall be used to separate items in the text file, and then click the button "Write results to text file...".

Using the Code

This application consists of two main parts:

  1. The MainWindow class does all the user interface stuff.
  2. The Searcher class provides the business logic for searching FileSystemInfo objects.

When the user clicks the "Start" button, the method Searcher.Start is executed. It starts a new thread called SearchThread. This thread searches for files and directories, matching the parameters that were entered by the user. If it finds a matching FileSystemInfo object, it raises an asynchronous FoundInfo event, so that the MainWindow can extract the FileSystemInfo object from the FoundInfoEventArgs, and update its results list. When the thread ends, it sets the m_thread member to null. The Searcher.Start method checks if m_thread is null every time it is executed, so there can never be more than one thread running at the same time.

When the user clicks the "Stop" button, the method Searcher.Stop is executed. It sets the m_stop member to true, so that the SearchThread can recognize this change and stop itself as soon as possible. Notice that this operation is thread-safe, because a boolean variable needs only one operation step to be set.

Important: In the Searcher_FoundInfo event handler, the MainWindow uses its Invoke method to invoke the this_FoundInfo method through a delegate. This way, the MainWindow makes sure that the code for updating the results list is executed in the MainWindow's thread, and not in the Searcher's thread. Calling the this_FoundInfo method directly would cause the application to crash, because the Searcher_FoundInfo event handler is not synchronized to the GUI controls.

private delegate void FoundInfoSyncHandler(FoundInfoEventArgs e);
private FoundInfoSyncHandler FoundInfo;

...

private void MainWindow_Load(object sender, EventArgs e)
{
    ...
    this.FoundInfo += new FoundInfoSyncHandler(this_FoundInfo);
    ...
}

...

private void Searcher_FoundInfo(FoundInfoEventArgs e)
{
    if (!m_closing)
    {
        this.Invoke(FoundInfo, new object[] { e });
    }
}

private void this_FoundInfo(FoundInfoEventArgs e)
{
    CreateResultsListItem(e.Info);
}

The CreateResultsListItem method creates and adds a new ListViewItem to the results list, which shows the data contained in the FilesystemInfo object. A FileSystemInfo object can be either be a FileInfo or a DirectoryInfo, depending on what the Searcher has found. The is operator can be used to decide what kind of object it is. If it's a FileInfo object, the list shall display the file's size in KB:

ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
if (info is FileInfo)
{
    lvsi.Text = GetBytesStringKB(((FileInfo)info).Length);
}
else
{
    lvsi.Text = "";
}

When the Searcher's thread ends, it raises a ThreadEnded event, so the MainWindow recognizes when the search has ended. The Searcher_ThreadEnded event handler uses the Invoke method the same way as the Searcher_FoundInfo event handler:

private delegate void ThreadEndedSyncHandler(ThreadEndedEventArgs e);
private ThreadEndedSyncHandler ThreadEnded;

...

private void MainWindow_Load(object sender, EventArgs e)
{
    ...
    this.ThreadEnded += new ThreadEndedSyncHandler(this_ThreadEnded);
    ...
}

...

private void Searcher_ThreadEnded(ThreadEndedEventArgs e)
{
    if (!m_closing)
    {
        this.Invoke(ThreadEnded, new object[] { e });
    }
}

private void this_ThreadEnded(ThreadEndedEventArgs e)
{
    EnableButtons();
    if (!e.Success)
    {
        MessageBox.Show(e.ErrorMsg,
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
    }
}

Points of Interest

You can use this code as a simple example for a multithreading application.

History

  • April 6, 2009
    • Published at CodeProject
  • April 8, 2009
    • Change: Uses the Form.Invoke method instead of a System.Timers.Timer
    • New feature: "Open Containing Folder"
  • April 24, 2009
    • Bug-fix: Solved a dead lock bug that sometimes occured when the user clicks the "Stop" button.
    • New feature: Supports multiple file names, separated by ";". Example: "*.wav;*.mp3".
  • September 29, 2012
    • Upgraded solution to Visual Studio 2012. 
    • New feature: Write the search results to a text file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Manfred Bittersam
Software Developer
Austria Austria
Manfred Bittersam has been developing industrial software
in C++ (MFC), C# and VB.NET for several years.
He has gone through a lot of changes, in career as well as in private life.
He says he has learned a lot from his mistakes Wink | ;)
Enjoy his free articles!

Comments and Discussions

 
QuestionLike Windows Search? [modified] PinmemberWilliam Vincent9-May-14 7:57 
GeneralMy vote of 4 PinmemberJasmine25012-May-13 12:53 
GeneralMy vote of 5 PinmemberAbinash Bishoyi8-Apr-13 7:10 
Questioninsert more functions on ColumnHeader for ListView [modified] PinmemberMember 13044745-Mar-13 18:36 
Questioncan we display matching lines from files in another grid on selection of file ? Pinmemberl6it89613-Jan-13 6:58 
AnswerRe: can we display matching lines from files in another grid on selection of file ? PinmemberManfred Bittersam14-Jan-13 7:05 
GeneralI vote 5 PinmemberZimnnsRichie1-Oct-12 17:45 
GeneralMy vote of 5 PinmemberSergio Andrés Gutiérrez Rojas29-Sep-12 13:17 
SuggestionOutput File Results [modified] Pinmemberkittell24-Sep-12 4:59 
GeneralRe: Output File Results PinmemberManfred Bittersam24-Sep-12 6:50 
GeneralRe: Output File Results Pinmemberkittell24-Oct-12 8:11 
GeneralMy vote of 5 PinmemberJαved28-May-12 20:32 
QuestionHow Can we use this application for searching file in a particular server from a website hosted in intranet. PinmemberRagigopi2-Dec-11 19:35 
AnswerRe: How Can we use this application for searching file in a particular server from a website hosted in intranet. PinmemberManfred Bittersam2-Dec-11 22:26 
QuestionHello; question about vbnet.. Pinmemberfuujinn28-Aug-11 21:20 
AnswerRe: Hello; question about vbnet.. PinmemberManfred Bittersam29-Aug-11 6:51 
QuestionHow to highlight the file with "Open containing folder" Pinmemberclefranc25-Jan-10 2:34 
AnswerRe: How to highlight the file with "Open containing folder" PinmemberManfred Bittersam25-Jan-10 8:06 
GeneralGreat App! - Can you add multiple search PinmemberBlazeDoggie12-May-09 6:16 
GeneralRe: Great App! - Can you add multiple search PinmemberManfred Bittersam12-May-09 8:30 
GeneralGood article PinmemberDonsw11-May-09 5:23 
GeneralBackgroundWorker Pinmemberdybs3-May-09 5:59 
GeneralRe: BackgroundWorker PinmemberManfred Bittersam3-May-09 6:34 
GeneralMultiple file types Pinmemberrctaubert27-Apr-09 3:12 
GeneralMultiple file types Pinmemberrctaubert14-Apr-09 10:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 29 Sep 2012
Article Copyright 2009 by Manfred Bittersam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid