Click here to Skip to main content
13,143,693 members (29,893 online)
Click here to Skip to main content
Add your own
alternative version


119 bookmarked
Posted 6 Apr 2009

File Searcher in C#

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


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)

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);
    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)
    if (!e.Success)

Points of Interest

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


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


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!

You may also be interested in...

Comments and Discussions

SuggestionPossibly another useful feature Pin
Member 1263920617-Jul-16 4:37
memberMember 1263920617-Jul-16 4:37 
GeneralMy vote of 5 Pin
Member 1263920617-Jul-16 4:34
memberMember 1263920617-Jul-16 4:34 
QuestionFile Searcher in c # Pin
Member 1129966113-May-15 10:41
memberMember 1129966113-May-15 10:41 
QuestionDirectory Lister Pin
Member 104109723-Jan-15 1:58
memberMember 104109723-Jan-15 1:58 
QuestionLike Windows Search? Pin
William Vincent9-May-14 7:57
memberWilliam Vincent9-May-14 7:57 
GeneralMy vote of 4 Pin
Jasmine25012-May-13 12:53
memberJasmine25012-May-13 12:53 
GeneralMy vote of 5 Pin
Abinash Bishoyi8-Apr-13 7:10
memberAbinash Bishoyi8-Apr-13 7:10 
Questioninsert more functions on ColumnHeader for ListView Pin
Member 13044745-Mar-13 18:36
memberMember 13044745-Mar-13 18:36 
Questioncan we display matching lines from files in another grid on selection of file ? Pin
l6it89613-Jan-13 6:58
memberl6it89613-Jan-13 6:58 
AnswerRe: can we display matching lines from files in another grid on selection of file ? Pin
Manfred Bittersam14-Jan-13 7:05
memberManfred Bittersam14-Jan-13 7:05 
GeneralI vote 5 Pin
ZimnnsRichie1-Oct-12 17:45
memberZimnnsRichie1-Oct-12 17:45 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas29-Sep-12 13:17
memberSergio Andrés Gutiérrez Rojas29-Sep-12 13:17 
SuggestionOutput File Results Pin
kittell24-Sep-12 4:59
memberkittell24-Sep-12 4:59 
GeneralRe: Output File Results Pin
Manfred Bittersam24-Sep-12 6:50
memberManfred Bittersam24-Sep-12 6:50 
GeneralRe: Output File Results Pin
kittell24-Oct-12 8:11
memberkittell24-Oct-12 8:11 
GeneralMy vote of 5 Pin
Jαved28-May-12 20:32
memberJαved28-May-12 20:32 
QuestionHow Can we use this application for searching file in a particular server from a website hosted in intranet. Pin
Ragigopi2-Dec-11 19:35
memberRagigopi2-Dec-11 19:35 
AnswerRe: How Can we use this application for searching file in a particular server from a website hosted in intranet. Pin
Manfred Bittersam2-Dec-11 22:26
memberManfred Bittersam2-Dec-11 22:26 
QuestionHello; question about vbnet.. Pin
fuujinn28-Aug-11 21:20
memberfuujinn28-Aug-11 21:20 
AnswerRe: Hello; question about vbnet.. Pin
Manfred Bittersam29-Aug-11 6:51
memberManfred Bittersam29-Aug-11 6:51 
QuestionHow to highlight the file with "Open containing folder" Pin
clefranc25-Jan-10 2:34
memberclefranc25-Jan-10 2:34 
AnswerRe: How to highlight the file with "Open containing folder" Pin
Manfred Bittersam25-Jan-10 8:06
memberManfred Bittersam25-Jan-10 8:06 
GeneralRe: How to highlight the file with "Open containing folder" Pin
javedsmart24-Jun-15 3:17
memberjavedsmart24-Jun-15 3:17 
GeneralGreat App! - Can you add multiple search Pin
BlazeDoggie12-May-09 6:16
memberBlazeDoggie12-May-09 6:16 
GeneralRe: Great App! - Can you add multiple search Pin
Manfred Bittersam12-May-09 8:30
memberManfred Bittersam12-May-09 8:30 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 29 Sep 2012
Article Copyright 2009 by Manfred Bittersam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid