Click here to Skip to main content
11,711,537 members (83,464 online)
Click here to Skip to main content

Monitor Multiple Files using FileSystemWatcher

, 7 Aug 2007 CPOL 29.5K 972 17
Rate this:
Please Sign up or sign in to vote.
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:

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()
        {
            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)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

You may also be interested in...

Comments and Discussions

 
QuestionService Version? Pin
Member 1094590214-Jul-14 9:06
memberMember 1094590214-Jul-14 9:06 
QuestionNot Working Pin
Gopichand.Jasoos9-May-14 1:56
memberGopichand.Jasoos9-May-14 1:56 
GeneralMy vote of 3 Pin
vipz00730-Dec-13 17:21
membervipz00730-Dec-13 17:21 
GeneralSecurityException unhandled error Pin
JKJKJK18-Oct-07 6:57
memberJKJKJK18-Oct-07 6:57 
GeneralThats exact what i need, but... Pin
Gh0stman8-Aug-07 8:33
memberGh0stman8-Aug-07 8:33 
GeneralRe: Thats exact what i need, but... Pin
Raymund Macaalay8-Aug-07 10:53
memberRaymund Macaalay8-Aug-07 10:53 

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 | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid