简体   繁体   English

删除文件后,FileWatcher触发更改事件

[英]FileWatcher fires change event after file deletion

I'm using a FileWatcher to trigger processing of files as soon as they are added to a folder. 将文件添加到文件夹后,我正在使用FileWatcher触发文件处理。 After the file it is processed it is deleted. 处理完文件后,将其删除。

My problem is that after the file is deleted I get another file change event which is so close to the deletion than in some cases checking for File.Exists it tells that the file still exists. 我的问题是,在删除文件后,我收到另一个文件更改事件,该事件与删除非常接近,因此在某些情况下不检查File.Exists会表明该文件仍然存在。 But of course some milliseconds later when looking to process the file it does not really exists. 但是,当然,在几毫秒之后,当您寻求处理文件时,它实际上并不存在。 The FileWatcher is set to notify on NotifyFilters.FileName | FileWatcher设置为通知NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.Size | NotifyFilters.Attributes NotifyFilters.Attributes

Thanks, florin 谢谢,弗洛林

If you take a little search on SO, you'll find plenty of questions, where people like to check if they are able to access a file or if they have the rights needed. 如果您在SO上进行一些搜索,就会发现很多问题,人们喜欢在这里检查他们是否能够访问文件或是否具有所需的权限。

The problem is, that even if you check it (like you do with File.Exists() ) the situation can be changed when it comes to the real operation. 问题是,即使您进行了检查(就像对File.Exists() ),在进行实际操作时情况也可以更改。

So just throw out the File.Exists() and put a try catch around your deletion operation. 因此,只需丢弃File.Exists()try catch删除操作即可。
If it fails, it's up to you to decide if you just drop it silently, inform the user, close your application, shutdown the pc, etc. 如果失败,则由您决定是否只是静默丢弃它,通知用户,关闭应用程序,关闭计算机等。

usually filewatcher fires multiple times since the fileChange is a series of events. 通常,由于fileChange是一系列事件,因此filewatcher会多次触发。 Here is a useful link. 这是一个有用的链接。 http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx Scroll to the end. http://msdn.microsoft.com/zh-cn/library/system.io.filesystemwatcher.aspx滚动到末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM