How to monitor a directory for renamed files

To monitor a directory for renamed files, follow this procedure:

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

property.

2. Register for the Renamed event.

3. Turn on events by setting EnableRaisingEvents to true.

The following code snippet demonstrates this process:


C#

TO create new watcher object


FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"c:\";


// Register for events


watcher.Renamed +=

new RenamedEventHandler(watcher_Renamed);


// Start Watching



watcher.EnableRaisingEvents = true;



// Event Handler



static void watcher_Renamed(object sender,

RenamedEventArgs e)

{

Console.WriteLine("Renamed from {0} to {1}",

e.OldFullPath,

e.FullPath);

}


Comments

No responses found. Be the first to comment...


  • 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: