Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / Visual Basic

Synchronize up to seven folders with System.IO.FileSystemWatcher in .NET

Rate me:
Please Sign up or sign in to vote.
1.21/5 (12 votes)
7 Jun 2007CPOL 35.7K   626   23   6
An app to synchronize up to seven folders with System.IO.FileSystemWatcher in .NET.

Introduction

With this app, you can monitor seven folders without any CPU loss .. and add up to seven folders to mirror in real time!

This application can also manage directories and subdirectories and the operations Add, Delete, Edit etc.

Background

The solution uses the WatchFolder class from the .NET Framework.

VB
watchfolder.Path = source12.Text
watchfolder.Filter = "*.*" ' filter1.Text
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
watchfolder.IncludeSubdirectories = True
AddHandler watchfolder.Renamed, AddressOf logchanger
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
watchfolder.EnableRaisingEvents = True

Using the Code

Add these Imports statements:

VB
Imports Microsoft.Win32
Imports System.IO

Here is how we use the code:

VB
AddHandler watchfolder.Renamed, AddressOf logchanger
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange

Points of Interest

The app can save all input from the user the first time inside the Registry.

History

  • Version 1.1.

License

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


Written By
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Michael B. Hansen6-May-10 1:07
Michael B. Hansen6-May-10 1:07 
GeneralSome background info would have been nice Pin
Andreas Kroll12-Jun-07 13:03
Andreas Kroll12-Jun-07 13:03 
Hi GC9N!

Apart from your really long explanation of the subject Laugh | :laugh: I missed some background information on reliability, etc..

If you get really busy doing file operations the internal buffers for the Watchfolder processing can overflow and you will start to loose information on some items. You can even get disconnected from the Watchfolder processing at all.

And please try to remember that people try to learn from articles in CodeProject.
If you submit an article with no explanation at all and sourcecodes with no comments in it you don't excactly contribute to that idea...

Keep on going... Wink | ;)


I'll be back.... Cool | :cool:
GeneralRe: Some background info would have been nice Pin
Alberto Venditti13-Jun-07 23:06
Alberto Venditti13-Jun-07 23:06 
QuestionWhy seven ? Pin
danidanidani7-Jun-07 7:59
danidanidani7-Jun-07 7:59 
AnswerRe: Why seven ? Pin
GC9N7-Jun-07 8:24
GC9N7-Jun-07 8:24 
JokeRe: Why seven ? Pin
danidanidani7-Jun-07 9:39
danidanidani7-Jun-07 9:39 

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.