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...

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.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.