It is more a question of what is the best practice in implementing this.
I have a FileSystemWatcher that should let me know about user changes in files and folders. Subdir is also observed. In the same directory, my program also sometimes changes. I do not want FileSystemWatcher detect events during these program changes.
My first implementation was a list where I can add the expected events. When I receive a file system event, I check the list and ignore it if there is one. It doesn't seem very reliable, but it seems to work.
Now I discovered a real problem:
D: Browse through FileSystemWatcher .
I have two folders: D: \ folder1 \ folder2
Now I want to delete folder1 (with folder2 in it) with my application. So I put D: \ folder1 in my delete list. Then I call something like Directory.Delete(@"D:\folder1", true) . Now I notice that folder1 cannot be deleted (for some reason) due to an exception. I delete the delete entry from my list, but folder2 has already been deleted and I get its FileSystemEvent. Therefore, I get a FileSystem event for D: \ folder1 \ folder2. My program thinks the user has deleted this folder and is doing the wrong thing.
I got some ideas:
1.) delete a folder recursively, deleting each file and each folder independently. With this, I get for each subfolder and write my own entry in the list. I have already implemented it, but very very slowly.
2.) Maybe there is a better way to have smart filters in FileSystemWatcher to make my list obsolete?
3.) Perhaps you can only delete the directory tree, if you can delete everything. Therefore, if this fails, I still have everything, and if not everything is deleted. This seems to be the most elegant solution for me, but I donβt know if it is possible at all?
4.) Is it possible to exclusively block all files and folders using my software? If this is all right, should it be possible to delete everything with a single delete command or something like that?
I am also open to other additional solutions.
Edit 1 to make it clearer:
I want to "see" user actions in a folder. If I will manipulate things from my program here, I do not want to see these events.
In my implementation, I get events for subfolders if the folder is locked and cannot be deleted.
It is not so easy to explain in English, because I am not a native speaker of English;).
Edit 2:
5.) Maybe you can filter all the events from a specific process in FileSystemWatcher ?