Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Folder Listener: A Tool to Watch File System Activity in any Folder

2.78/5 (13 votes)
27 Jan 2008CPOL1 min read 1   3.4K  
This tool is for the tester and developer to track any file system activity to a particular folder

Introduction

Most of the time when you are doing some development or testing, you might need to know what changes are made in a particular folder. This requirement frequently arises when you are testing /developing software that copies /delete files in the background without user intervention; for example background update and installation/un-installation of software product. It can also be useful when you want to know the intermediate file that a particular application creates in a particular folder.

Image 1

Technology Used

This application is based on Microsoft's .NET Framework 2.0 and is written in C#. .NET Framework provides a component FileSystemWatcher that is used to track and listen to the changes made in a particular folder. You may find several articles on this component, so I am not going to explain the working code. Below is the code segment that allows you to create a FileSystemWatcher.

Using the Code

It uses the FileSystemWatcher component of the .NET Framework. The main code snippet is given below:

C#
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = textBox1.Text;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                   NotifyFilters.DirectoryName | NotifyFilters.FileName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;

Tool Limitations

This tool is designed to listen to a single folder at a time. However in future, I will update it for multiple folders.

Using the Tool

Use of this tool is very simple. Just run the tool and select the folder you want to listen to or track.

History

  • 27th January, 2008: Initial post

License

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