C# - 在文件夹predict文件系统事件删除文件系统、文件夹、事件、predict

2023-09-03 05:08:04 作者:晚风抚人

这是更多的是关于什么是在实施这一目标的最佳做法的问题。

This is more a question about what's the best practice in implementing this.

我有一个 FileSystemWatcher的这应该告诉我有关的文件和文件夹的用户的变化。此外子目录的关注。在同一目录下我的程序也做有时会变化。我不想让 FileSystemWatcher的来检测这些计划的变更事件。

I have a FileSystemWatcher which should inform me about User's changes on files and folders. Also subdirs are watched. In the same directory my program does also sometimes changes. I don't want the FileSystemWatcher to detect events on these program changes.

我的第一个执行是一个列表,我可以添加预期的事件。当我得到一个文件系统事件我检查列表,忽略它,如果它的存在。这听起来是不是很强劲,但它似乎工作。

My first implementation was a list where I can add expected events. When I get a file system event I check the list and ignore it if it's there. This doesn't sound very robust but it seemed to work.

现在我发现真正的问题: D:由 FileSystemWatcher的 观看 我有两个文件夹,这样的:D:\文件夹1 \ FOLDER2 现在我想删除文件夹1(与FOLDER2的话)与我的应用程序。所以我把D:\文件夹1在我删除列表。然后,我打电话像 Directory.Delete(@D:\文件夹1,真)。现在我注意到,文件夹1无法删除(为什么曾经)在一个例外。我从我的列表中删除的删除条目,但FOLDER2已经删除,我得到他的FileSystemEvent。所以,我得到一个文件系统事件D:\文件夹1 \ FOLDER2。我的程序认为现在用户已删除该文件夹,并做了错误的事情。

Now I detected the real problem: D: is watched by the FileSystemWatcher. I have two folders like that: 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 can't be deleted (why ever) over an exception. I remove the delete entry from my list but folder2 was already deleted and I get his FileSystemEvent. So I get a FileSystem event for D:\folder1\folder2. My program thinks now the user has deleted this folder and is doing the wrong things.

我有一些想法,现在:

1)递归地删除每个文件和每个文件夹由它自己删除该文件夹。有了这个,我得为每个子文件夹和文件的自己的列表条目。我已经实现了它,但它是非常非常非常慢。

1.) recursively delete the folder by deleting every file and every folder by it's own. With this I get for every subfolder and file an own list entry. I already implemented it but it is very very very slow.

2。)也许有更好的方法,有聪明的过滤器 FileSystemWatcher的来使我的名单过时了吗?

2.) Maybe there is a better way to have clever filters in the FileSystemWatcher to make my list obsolete?

3。)也许有可能只删除一个目录树,如果有可能,删除一切。所以,如果失败的话我还有一切,如果不是一切都将被删除。这似乎是对我来说最完美的解决方案,但不知道这是否可能?

3.) Maybe it is possible to only delete a directory tree if it's possible to delete everything. So if it fails I still have everything and if not everything is deleted. This seems to be the most elegant solution for me but no idea if this is even possible?

4。)是否有可能完全锁住我的软件的所有文件和文件夹?如果此去确定它应该可以删除一切与一个delete命令,或在某种程度上也是这样吗?

4.) Is it possible to exclusively lock all files and folders by my software? If this this went ok it should be possible delete everything with one delete command or somehow like this?

我也开放给其他其他解决方案。

I'm also open for other additional solutions.

编辑1,使之更加清楚:

Edit 1 to make it more clear:

我只是想看上的文件夹的用户操作。如果我在这里处理事情从我的节目,我不希望看到这样的活动。

I only want to "see" user actions on a folder. If I manipulate things from my program in here I don't want to see this events.

使用我的执行,我得到的子文件夹的事件,如果一个文件夹被锁定,不能被删除。

With my implementation I get events for subfolders if a folder is locked and can't be deleted.

这不是那么容易的英文解释,因为我不是英语母语的人。)

It's not so easy to explain in english because I'm no english native speaker ;).

编辑2:

5。)也许有可能在 FileSystemWatcher的过滤从定义的过程中的所有事件?

5.) Maybe it is possible to filter in FileSystemWatcher all events from a defined process?

推荐答案

确定这里是我的问题的解决方案:

Ok here is my solution of the problem:

只是为了删除命令:

我实现2列表,一个用于文件删除,一个用于文件夹删除。

I implemented 2 lists, one for file deletes and one for folder deletes.

文件列表entrys没有超时。如果我删除了一个文件创建一个列表项,如果我得到预期的删除事件我删除该条目和以前一样。

file list entrys have no timeout. If I delete a file I create a list entry and if I get the expected delete event I remove the entry as before.

文件夹列表entrys没有超时后创建。我可以在第二通过一种特殊的方法后手动命令他们超时。如果我删除一个文件夹我添加的DeleteFolder列表条目。从该文件夹或文件或子文件夹或subfolderfile每个删除事件,是因为这个文件夹删除条目被忽略。后删除完成我手臂的超时时间的DeleteFolder项。如果删除抛出一个异常,我也一样。所以我希望得到第二次之后的所有事件。所以我忽略所有的事件,如果删除命令的工作与否。在此之后,的DeleteFolder列表条目被删除。

folder list entrys have no timeout after create. I can command them manually to time out after a second by a special method. If I delete a folder I add a deleteFolder list entry. Every delete event from this folder or file or subfolder or subfolderfile is ignored because of this delete folder entry. After the delete is finished I arm the timeout for the deleteFolder entry. If the delete throws an exception I do the same. So I expect to get all events after a second. So I'm ignoring all events if the delete command works or not. After this the deleteFolder list entry gets deleted.

限制: 1.全部删除事件一定要来在一秒钟后删除发生。 2.不允许删除这样的文件夹: 删除文件夹(已完成) 再次创建文件夹 等不到1秒 再删除文件夹(超时未完成删除文件夹列表项)

Limitations: 1. all delete events have to come in one second after the delete happened. 2. it is not allowed to delete a folder like this: delete folder (completed) create folder again wait less then 1 second delete folder again (timeout has not finished of the delete folder list entry)

希望这有助于有人甚至是非常复杂的^^

Hope this helps somebody even it is very complicated ^^

 
精彩推荐
图片推荐