Click here to Skip to main content
12,759,139 members (33,773 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 7 Aug 2007

Monitor Multiple Files using FileSystemWatcher

, 7 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to monitor multiple files using FileSystemWatcher.

Screenshot - Multiple_File_Monitor.jpg


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:

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:

namespace MonitorFolderActivity
    public partial class Form1 : Form
        public Form1()

        private void btnRun_Click(object sender, EventArgs e)
            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);
                        oFileWatcher.Changed -= 
                          new System.IO.FileSystemEventHandler(FileSystemWatcherUpdated);

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

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

                    oFileWatcher.EnableRaisingEvents = true;

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

        private delegate void AppendListHandler(string sLog);
        private void AppendText(string sLog)
            if (lstResultLog.InvokeRequired)
                lstResultLog.Invoke(new AppendListHandler(AppendText), 
                                    new object[] { sLog });
                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;



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


About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand

You may also be interested in...


Comments and Discussions

GeneralCannot watch all selected folders using FileSystemWatcher c# Pin
Member 1196746427-Sep-15 21:01
memberMember 1196746427-Sep-15 21:01 
QuestionService Version? Pin
Member 1094590214-Jul-14 10:06
memberMember 1094590214-Jul-14 10:06 
QuestionNot Working Pin
Gopichand.Jasoos9-May-14 2:56
memberGopichand.Jasoos9-May-14 2:56 
GeneralMy vote of 3 Pin
vipz00730-Dec-13 18:21
membervipz00730-Dec-13 18:21 
GeneralSecurityException unhandled error Pin
JKJKJK18-Oct-07 7:57
memberJKJKJK18-Oct-07 7:57 
GeneralThats exact what i need, but... Pin
Gh0stman8-Aug-07 9:33
memberGh0stman8-Aug-07 9:33 
GeneralRe: Thats exact what i need, but... Pin
Raymund Macaalay8-Aug-07 11:53
memberRaymund Macaalay8-Aug-07 11:53 

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
Web02 | 2.8.170217.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid