FileSystemWatcher event on adding a folder with subdirectories

Dan picture Dan · Jul 27, 2011 · Viewed 10.3k times · Source

I'm using a FileSystemWatcher to monitor a directory for new folders created. This would work just fine, except that each of these folders can have one or more subdirectories inside of them.

The problem is that when I copy the top level folder (with one or more subdirectories in it) the FileSystemWatcher only catches the addition of the top level folder and not the subdirectories.

Ex: Drag folder 'foo' with subdirectory 'bar' into directory being watched FileSystemWatcher notifies that new directory 'foo' was created, but says nothing about 'bar'

Am I missing something with the functionality of FileSystemWatcher or is there some other way around this?

Answer

Austin Salonen picture Austin Salonen · Jul 27, 2011

You also need to handle OnRenamed, as well as IncludeSubdirectories.

From MSDN:

Copying and moving folders

The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.

To be notified that the contents of folders have been moved or copied into a watched folder, provide OnChanged and OnRenamed event handler methods...