0

How exactly do you distinguish file and folder deletion events using the file system watcher control?

I've searched google trying to find an answer, but the only suggestion I've seen is to use separate filesystemwatcher objects for each task, just changing the "include sub directories" flag each time. That's stupid, there must be a simpler solution for such a simple control...

Edited by Jaydenn: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Ketsuekiame
0

My first instinct was to say test the FullPath property of the FileSystemEventArgs.
But, since it has been deleted, it no longer exists!:@

Although no guarantee, you could test the FullPath (or Name) property for an extension.
If it is string.Empty then it is probably a folder.

0

Unfortunately this is getting less and less likely as extensions can now be varying length and a lot of file/folder names contains periods.

The only other method is to make an in memory list at startup of the folder you're watching. So create a "WatchedFile" object which has an "IsDirectory" property.
When you start your app, you get a list of the folder contents (which will include directories) and get the file properties for each one. Then go through each sub directory (if you're monitoring them) and do the same.

Then when something gets deleted, you will receive the fullpath of the deleted item. Check this against your in memory list.

Although a more accurate check than Nick's, it's much more time consuming (for large lists, comparitively speaking).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.