Click here to Skip to main content
Licence CPOL
First Posted 7 Aug 2007
Views 16,860
Downloads 283
Bookmarked 13 times

Monitor Multiple Files using FileSystemWatcher

By | 7 Aug 2007 | Article
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)

About the Author

Raymund Macaalay

Technical Lead

New Zealand New Zealand

Member

Follow on Twitter Follow on Twitter
http://nz.linkedin.com/in/macaalay
http://anyrest.wordpress.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSecurityException unhandled error PinmemberJKJKJK6:57 18 Oct '07  
GeneralThats exact what i need, but... PinmemberGh0stman8:33 8 Aug '07  
GeneralRe: Thats exact what i need, but... PinmemberRaymund Macaalay10:53 8 Aug '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid