FileSystemWatcher不会报告锁定文件中的更改 [英] FileSystemWatcher does not report changes in a locked file

查看:83
本文介绍了FileSystemWatcher不会报告锁定文件中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileSystemWatcher监视文件夹,如下所示:

I'm monitoring a folder using a FileSystemWatcher like this:

watcher = new FileSystemWatcher(folder);
watcher.NotifyFilter = NotifyFilters.Size;
watcher.Changed += changedCallback;

当我在记事本中的该文件夹中打开一个新文件并将其保存时,会收到通知.如果我继续写然后保存,则会收到通知.如果我关闭文件并保存,则会收到通知.正是我想要的.

When I open a new file in notepad in that folder and save it, I get a notification. If I keep writing and then I save, I get a notification. If I close the file with saving it, I get a notification. Exactly what I wanted.

但是,事实证明,如果我在该文件夹中创建一个文件并将其共享模式设置为FileShare.Read,然后对其进行写入,则在该文件关闭之前,我不会收到任何通知.另一个解决方法是打开文件(例如在记事本中),这显然会导致其状态被更新,然后我的监视应用程序会收到通知.另一个解决方法是我可以在Windows资源管理器中进行刷新,这再次导致文件状态被更新.

However, it turns out that if I create a file in that folder and I set its sharing mode to FileShare.Read, and then I write to it, I will not get any notifications until the file is closed. Another workaround is to open the file (e.g. in Notepad), which apparently causes its state to be updated, and then my monitoring application gets the notification. Yet another workaround is a refresh I can do in Windows Explorer, which again causes the file state to be updated.

有趣的是,如果在进行更改时查看Windows资源管理器,则会发现:

Interestingly, if I look at Windows Explorer while I make the changes, I notice that:

  1. 如果文件共享以供阅读&写入时,保存后,它的大小将立即在Windows资源管理器中更新.
  2. 如果共享文件仅供读取,则除非手动刷新窗口,否则文件大小不会在Windows资源管理器中立即更新.

因此,看来我的监视应用程序与Windows资源管理器具有相同的行为.我当时正在考虑运行一个仅扫描文件夹中文件的线程,但是我想知道在这种情况下是否还有其他更优雅的方法.

So it seems that my monitoring application shares the same behavior as Windows Explorer. I was thinking about running a thread that will just scan the files in the folder, but I'm wondering if there's anything more elegant to be done in this case.

顺便说一句,我正在使用Win7,但不确定在其他Windows版本上是否也会发生此问题.

BTW, I'm using Win7 and I'm not sure that this problem happens on other Windows versions as well.

谢谢!

编辑:在C ++中使用ReadDirectoryChanges可以得到完全相同的结果.实施我之前讨论的线程也没有帮助.我想知道Windows资源管理器中的F5实际在做什么,因为它确实会报告更改.

EDIT: Using ReadDirectoryChanges in C++ got me the same exact results. Implementing the thread I was talking about earlier didn't help as well. I'm wondering what is F5 in Windows Explorer actually doing, because it does cause the change to be reported.

推荐答案

此问题的解决方案不是打开文件,而是实际读取文件.读取一个字节就足够了,Windows缓存机制会将文件的内容写入磁盘,从而使您可以读取它们.

The solution to the problem is not to open the files, but to actually READ from them. It's enough to read even one byte, and the Windows cache mechanism will write the contents of the file to the disk, thus allowing you to read them.

我最终实现了一个遍历所有文件,打开它们并从其中读取一个字节的线程.这导致它们发生更改,并在FileSystemWatcher对象中触发了事件.

I ended up implementing a thread that went over all the files, opened them and read a byte from them. This caused them to change, and triggered the event in the FileSystemWatcher object.

Windows资源管理器F5也能正常工作的原因是Windows实际读取文件的内容以显示一些扩展内容(例如,缩略图).读取文件后,缓存将首先写入磁盘,从而在FSW中触发事件.

The reason that Windows Explorer F5 works as well, is that Windows actually reads the contents of the file in order to show some extended contents (e.g., thumbnails). Once the file is being read, the cache first writes to the disk, triggering the event in the FSW.

这篇关于FileSystemWatcher不会报告锁定文件中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆