Click here to Skip to main content
6,630,586 members and growing! (15,985 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » File System     Intermediate

Directory Mirror using the FileSystemWatcher class

By Luc Archambault

An application that monitors a directory and maintains a copy of it.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:10 Feb 2005
Views:64,914
Bookmarked:74 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 5.56 Rating: 4.52 out of 5
1 vote, 6.3%
1

2

3
4 votes, 25.0%
4
11 votes, 68.8%
5

The DirectoryMirror Application

This application creates and maintains a mirror of a selected directory; it monitors IO activity in the specified directory and reacts to this in order to keep a copy of the contents (including subfolders) in another directory. This is an implementation of the System.IO.FileSystemWatcher class.

I've created a class called directoryMirror which is based on the FileSystemWatcher class. The handlers for the events (changed, created, deleted, renamed) of this class do all the work of maintaining a copy of the "source" directory in a "mirror" directory. I've added an event to the class which sends messages about IO activity and various exceptions. I've also added a property called mirrorDirectory that points to a string containing the path of the mirror folder, and a second property called sourceDirectory which is nothing more than the Path property of the FileSystemWatcher class.

There are eight different NotifyFilters, and as you will notice, our directoryMirror class uses three of them: the FileName, DirectoryName, and LastWrite NotifyFilters. This determines what changes to monitor. The Filter property lets you select what kind of files you want to watch. Just set the value to a file extension, for example, ".txt" for text files. Our directoryMirror class' Filter property is set to an empty value "" so we'll be monitoring all files and directories, as well as subdirectories because the IncludeSubdirectories property is set to true.

It's a well known bug that file paths returned by the event arguments System.IO.FileSystemEventArgs lose their original casing and are all in lower case. This is not too bad since the operating system makes no difference in the casing of file paths.

Believe it or not, I actually had a practical use for this little application when I created it! I might also say that I created the need because I had been itching to experiment with the FileSystemWatcher class for a long time. I made minor changes to my original application to make it more educational. If the uses of this application are somewhat limited, I think the code can be helpful to anybody looking for an introduction to the FileSystemWatcher class.

The directoryMirror class

The directoryMirror class is presented here, but the download contains all the code, including the form.

using System;
using System.IO;

namespace mirror
{

    // This class is based on the FileSystemWatcher class

    public class directoryMirror: System.IO.FileSystemWatcher
    {
        // Path of the mirror directory

        string mirDir = "";

        // Source directory

        public string sourceDirectory
        {
            get { return this.Path; }
            set { this.Path = value; }
        }

        // Mirror Directory

        public string mirrorDirectory
        {
            get { return mirDir; }
            set { mirDir = value; }
        }

        // Delegate and event to send messages about IO activity

        // and various exceptions.

        public delegate void infoMessageDelegate(string infoMessage);
        public event infoMessageDelegate infoMessageEvent;

        public directoryMirror(string srcDir, string mirDir)
        {
            // Set up the path to the source directory

            sourceDirectory = srcDir;
            // Set up the path to the mirror directory

            mirrorDirectory = mirDir;
            
            // Set up the different properties

            // Monitor all files and directories

            this.Filter = "";
            // Listen for changes in the name of files and directories

            // and changes in date/time of the last modification.

            this.NotifyFilter = 
              ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName | 
                System.IO.NotifyFilters.DirectoryName | 
                System.IO.NotifyFilters.LastWrite)));
            this.IncludeSubdirectories = true;
            this.EnableRaisingEvents = true;

            // Set up the handlers for the FileSystemWatcher events

            this.Changed += new FileSystemEventHandler(fsw_onChanged);
            this.Created += new FileSystemEventHandler(fsw_onCreated);
            this.Deleted += new FileSystemEventHandler(fsw_onDeleted);
            this.Renamed += new RenamedEventHandler(fsw_onRenamed);    
        }

        private void fsw_onChanged(object sender, System.IO.FileSystemEventArgs e)
        {
            // Because of the NotifyFilters we are using, this event can only

            // pertain to a file, not a directory

            try
            {
                if(!System.IO.Directory.Exists(e.FullPath))
                    // It's a file

                {
                    // Destination to copy the file to

                    string destination = 
                      e.FullPath.Replace(sourceDirectory, mirrorDirectory);
                    // Overwrite or copy the file in the mirror directory

                    System.IO.File.Copy(e.FullPath, destination, true);
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + e.ChangeType + " " + e.FullPath);
                }
            }
            catch(DirectoryNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION " + 
                   "(onChanged): Directory No Found , " + iox.Message);
            }
            catch(FileNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                   " (onChanged): File Not Found, " + iox.Message);
            }
            catch(IOException iox)
            {
                infoMessageEvent("\r\nEXCEPTION " + 
                   "(onChanged): IO Error, " + iox.Message);
            }
            catch(Exception ex)
            {
                infoMessageEvent("\r\nEXCEPTION " + 
                   "(onChanged): " + ex.Message);
            }

        }

        private void fsw_onCreated(object sender, 
                     System.IO.FileSystemEventArgs e)
        {
            try
            {
                if(System.IO.Directory.Exists(e.FullPath))
                    // It's a directory

                {
                    // Create a matching directory in the mirror directory

                    System.IO.Directory.CreateDirectory(
                       e.FullPath.Replace(sourceDirectory, mirrorDirectory));
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + 
                                 e.ChangeType + ", " + e.FullPath);
                }
                else
                    // It's a file

                {
                    // Destination to copy the file to

                    string destination = 
                      e.FullPath.Replace(sourceDirectory, mirrorDirectory);
                    // Copy the file to the mirror directory

                    System.IO.File.Copy(e.FullPath, destination, true);
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
                }
            }
            catch(DirectoryNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                    " (onCreated): DIRECTORY NOT FOUND , " + iox.Message);
            }
            catch(FileNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION (onCreated): " + 
                    "FILE NOT FOUND, " + iox.FileName + ", " + iox.Message);
            }
            catch(IOException iox)
            {
                infoMessageEvent("\r\nEXCEPTION " + 
                    "(onCreated): IO ERROR, " + iox.Message);
            }
            catch(Exception ex)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                    " (onCreated): " + ex.Message);
            }

        }

        private void fsw_onDeleted(object sender, 
                     System.IO.FileSystemEventArgs e)
        {
            try
            {
                if(System.IO.Directory.Exists(
                  e.FullPath.Replace(sourceDirectory, 
                  mirrorDirectory)))
                // It's a directory

                {
                    System.IO.Directory.Delete(
                       e.FullPath.Replace(sourceDirectory, 
                       mirrorDirectory), true);
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
                }
                else
                    // It's a file

                {
                    string destination = 
                      e.FullPath.Replace(sourceDirectory, 
                      mirrorDirectory);
                    System.IO.File.Delete(destination);
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
                }
            }
            catch(DirectoryNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                    " (onDeleted): DIRECTORY NOT FOUND, " 
                    + iox.Message);
            }
            catch(FileNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION (onDeleted):" + 
                    " FILE NOT FOUND, " + iox.FileName + 
                    ", " + iox.Message);
            }
            catch(IOException iox)
            {
                infoMessageEvent("\r\nEXCEPTION " + 
                    "(onDeleted): IO ERROR, " + iox.Message);
            }
            catch(Exception ex)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                   " (onDeleted): " + ex.Message);
            }
        }

        private void fsw_onRenamed(object sender, RenamedEventArgs e)
        {
            try
            {
                if(System.IO.Directory.Exists(e.FullPath))
                    // It's a directory

                {
                    if(System.IO.Directory.Exists(
                       e.OldFullPath.Replace(sourceDirectory, 
                       mirrorDirectory)))
                    {
                        string oldFPath = 
                           e.OldFullPath.Replace(sourceDirectory, 
                           mirrorDirectory);
                        string newFPath = e.FullPath.Replace(sourceDirectory, 
                                                            mirrorDirectory);
                        System.IO.Directory.Move(oldFPath, newFPath);
                        // Send a message to report this activity

                        infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
                    }
                }
                else
                    // It's a file

                {
                    string oldFPath = 
                       e.OldFullPath.Replace(sourceDirectory, mirrorDirectory);
                    string newFPath = 
                       e.FullPath.Replace(sourceDirectory, mirrorDirectory);
                    System.IO.File.Move(oldFPath, newFPath);
                    // Send a message to report this activity

                    infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
                }
            }
            catch(DirectoryNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                    " (onRenamed): DIRECTORY NOT FOUND, " + iox.Message);
            }
            catch(FileNotFoundException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                   " (onRenamed): FILE NOT FOUND, " + iox.FileName + 
                   ", " + iox.Message);
            }
            catch(IOException iox)
            {
                infoMessageEvent("\r\nEXCEPTION" + 
                   " (onRenamed): IO ERROR, " + iox.Message);
            }
            catch(Exception ex)
            {
                infoMessageEvent("\r\nEXCEPTION (onRenamed): " 
                   + ex.Message);
            }
        }
    }
}

Points of Interest

By watching the messages sent back by the directoryMirror object, you'll quickly realize that the sequence of events that are raised is often not what you would expect. Also, exceptions are sometimes raised but the application manages to successfully complete the "faulty" task like nothing happened! An IO error like this: "The process cannot access the file "D:\_mysys\mirror\22\file_test.txt" because it is being used by another process" seems to be a classic. This seems to happen when there are many actions to take care of; dropping a folder containing many files and subfolders in the source directory will often trigger this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Luc Archambault


Member
I have been working with different web technologies (ASP, ASP.NET, ColdFusion, Perl/CGI) for the past 7 years. I live in Montreal, where I work for a little consultant group specializing in Microsoft technologies.
Occupation: Web Developer
Location: Canada Canada

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralThanks PinmemberEdward Ceballos5:59 4 Aug '08  
GeneralThank you Pinmembernirvansk81516:28 7 Apr '08  
GeneralWorks great, but we need a few tweaks. Pinmemberlmeyers18:04 15 Feb '08  
GeneralEvents firing twice Pinmemberp10004:45 6 Mar '07  
Generalnice work ~!great Pinmembernicesnow16:57 5 Feb '07  
Generalhow bout web application? Pinmember!ndera17:51 12 Dec '06  
GeneralRe: how bout web application? PinmemberLuc Archambault8:51 13 Dec '06  
GeneralMonitor a list of path Pinmemberpasdan23:41 22 May '06  
QuestionHow can we solve the IO exception? Pinmemberchallengerking18:06 19 Apr '06  
AnswerRe: How can we solve the IO exception? PinmemberLuc Archambault19:45 19 Apr '06  
GeneralRe: How can we solve the IO exception? Pinmemberchallengerking20:32 19 Apr '06  
GeneralRe: How can we solve the IO exception? Pinmemberchallengerking17:11 20 Apr '06  
AnswerRe: How can we solve the IO exception? PinmemberFayyaz Lodhi20:21 3 Jan '07  
AnswerRe: How can we solve the IO exception? Pinmembersides_dale21:23 14 Apr '07  
GeneralRe: How can we solve the IO exception? PinmemberFayyaz Lodhi21:28 15 Apr '07  
GeneralRe: How can we solve the IO exception? Pinmembersides_dale15:28 22 Apr '07  
GeneralRe: How can we solve the IO exception? PinmemberEpaxx12:28 15 Aug '07  
GeneralNice work Luc Pinmemberwconnors5:52 14 Nov '05  
GeneralDoesn't work for VS 2005 Beta 2 anymore PinmemberMike Wild4:45 14 Jun '05  
GeneralRe: Doesn't work for VS 2005 Beta 2 anymore Pinmemberflatsheep200512:32 18 May '06  
GeneralThank you Luc! PinsussAnonymous21:01 24 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Feb 2005
Editor: Smitha Vijayan
Copyright 2005 by Luc Archambault
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project