Click here to Skip to main content
15,891,704 members
Articles / Programming Languages / C#

Monitor Multiple Files using FileSystemWatcher

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
7 Aug 2007CPOL 62.1K   1.8K   20   8
How to monitor multiple files using FileSystemWatcher.

Screenshot - Multiple_File_Monitor.jpg

Introduction

This code will show how to monitor multiple files by creating multiple instances of FileSystemWatcher.

Using the code

To use the code, the following namespaces must be referenced:

C#
using System.IO; 
using System.Collections;

System.IO will be used for FileSystemWatcher and Directory, while System.Collections for the ArrayList. Now to the code:

C#
namespace MonitorFolderActivity
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            DisableButtons();
            bool bMonitorUpdated = chkMonitorUpdated.Checked;
            bool bMonitorDeleted = chkMonitorDeleted.Checked; 
            bool bMonitorCreated = chkMonitorCreated.Checked;

            ArrayList aFileWatcherInstance = new ArrayList();

            foreach (string sMonitorFolder in lstFolders.Items)
            {
                //Check if Directory Exisits
                if (Directory.Exists(sMonitorFolder))
                {
                    FileSystemWatcher oFileWatcher = new FileSystemWatcher();

                    //Set the path that you want to monitor.
                    oFileWatcher.Path = sMonitorFolder;

                    //Set the Filter Expression.
                    oFileWatcher.Filter = txtMonitorFileExpression.Text;

                    if (bMonitorUpdated)
                        oFileWatcher.Changed += 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherUpdated);
                    else
                        oFileWatcher.Changed -= 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherUpdated);

                    if (bMonitorDeleted)
                        oFileWatcher.Deleted += 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherDeleted);
                    else
                        oFileWatcher.Deleted -= 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherDeleted);

                    if (bMonitorCreated)
                        oFileWatcher.Created += 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherCreated);
                    else
                        oFileWatcher.Created -= 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherCreated);

                    oFileWatcher.EnableRaisingEvents = true;

                    //Add a new instance of FileWatcher 
                    aFileWatcherInstance.Add(oFileWatcher);
                }
            }
        }
        void FileSystemWatcherCreated(object sender, System.IO.FileSystemEventArgs e)
        {
            //A file has been deleted from the monitor directory.
            string sLog = "File Created: " + e.Name;
            AppendText(sLog);
        }
        void FileSystemWatcherDeleted(object sender, System.IO.FileSystemEventArgs e)
        {
            //A file has been deleted from the monitor directory.
            string sLog = "File Deleted: " + e.Name;
            AppendText(sLog);
        }
        void FileSystemWatcherUpdated(object sender, System.IO.FileSystemEventArgs e)
        {
            //A file has been deleted from the monitor directory.
            string sLog = "File Updated: " + e.Name;
            AppendText(sLog);
        }

        private delegate void AppendListHandler(string sLog);
        private void AppendText(string sLog)
        {
            if (lstResultLog.InvokeRequired)
                lstResultLog.Invoke(new AppendListHandler(AppendText), 
                                    new object[] { sLog });
            else
                lstResultLog.Items.Add(Convert.ToString(DateTime.Now) + 
                                       " - " + sLog);
        }

        private void DisableButtons()
        {
            chkMonitorUpdated.Enabled = false;
            chkMonitorDeleted.Enabled = false;
            chkMonitorCreated.Enabled = false;
            txtMonitorFileExpression.Enabled = false;
            btnRun.Enabled = false;

        }
    }
}

License

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


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
GeneralMy vote of 5 Pin
srilekhamenon4-Jul-23 21:07
professionalsrilekhamenon4-Jul-23 21:07 
GeneralCannot watch all selected folders using FileSystemWatcher c# Pin
Member 1196746427-Sep-15 20:01
Member 1196746427-Sep-15 20:01 
QuestionService Version? Pin
Member 1094590214-Jul-14 9:06
Member 1094590214-Jul-14 9:06 
QuestionNot Working Pin
Gopichand.Jasoos9-May-14 1:56
Gopichand.Jasoos9-May-14 1:56 
GeneralMy vote of 3 Pin
vipz00730-Dec-13 17:21
vipz00730-Dec-13 17:21 
GeneralSecurityException unhandled error Pin
JKJKJK18-Oct-07 6:57
JKJKJK18-Oct-07 6:57 
GeneralThats exact what i need, but... Pin
Gh0stman8-Aug-07 8:33
Gh0stman8-Aug-07 8:33 
GeneralRe: Thats exact what i need, but... Pin
Raymund Macaalay8-Aug-07 10:53
Raymund Macaalay8-Aug-07 10:53 
Hi it works, just download the full source and make sure that you have the proper Folders

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.