How to Monitor Directory for Created and Deleted file Changes

To monitor a directory for changes, follow this procedure:

1. Create a new FileSystemWatcher object, specifying the directory in the Path

property.

2. Register for the Created and Deleted events.

3. Turn on events by setting EnableRaisingEvents to true.

The following code snippet demonstrates this process:

create file system watcher object



FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"c:\";


// Register for events like creation and deletion or other as per your requirement


watcher.Created +=

new FileSystemEventHandler(watcher_Changed);

watcher.Deleted +=

new FileSystemEventHandler(watcher_Changed);


// To Start Watching set the property enableraising events as true


watcher.EnableRaisingEvents = true;


// Event Handler about changing and deletion event to do the necessary code when particular change at event occure

static void watcher_Changed(object sender,

FileSystemEventArgs e)

{

Console.WriteLine("Directory changed({0}): {1}",

e.ChangeType,

e.FullPath);

}


The event handler simply reports each change found in the FileSystemEventArgs object that is sent to the event handler.


Comments

Author: ABitSmart21 Dec 2009 Member Level: Gold   Points : 1

content match at http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/3152d304-2941-468d-bb89-d87f1571c021

Please format your resource.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: