监视正在读取的文件的文件夹
我正在尝试监视目录中的文件以确定文件何时打开/访问。我认为 FileSystemWatcher
可以使用事件 Changed
来实现这一目的。
问题是,某些应用程序不会在它们打开/访问的文件上创建锁定,也不会更改修改日期或访问日期(即使在fsutil behavior set disablelastaccess 0
之后)。以记事本为例。显然,它会在内存中复制该文件并在那里播放,直到您保存它。它也不会更新访问日期。
如何监视文件目录并在任何程序(例如记事本)打开/访问文件时收到通知?文件可以从另一台计算机打开,不一定是在运行“观察者”。
我发现了很多类似的问题,但没有看到一个关注文件“访问”的问题。
I am trying to watch files in a directory to determine when files are opened/accessed. I thought FileSystemWatcher
would do the trick using the event Changed
.
Problem is that some applications do not create a lock on the file they open/access or change either the date modified or date accessed (even after fsutil behavior set disablelastaccess 0
). Notepad for example. Apparently is makes a copy of the file in memory and plays with it there until you save it. Nor does it update the Date Accessed.
How can I monitor a directory of files and be notified when a file is simply opened/accessed by any program (e.g. Notepad)? Files may be opened from another computer, not necessarily on the computer running the "watcher".
I found lots of similar questions but did not see one focusing on file "access".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是很正常的。更新现有文件非常危险,因为它可能会导致无法挽回的数据丢失。写入时发生磁盘错误(例如磁盘已满)是非常坏的消息。使用的常见算法:
显然这不会导致更改要引发的事件,没有文件被更改。
抱歉,我没有仔细阅读问题。对于仅打开文件进行读取的应用程序来说,没有任何通知。 FSW 只能检测文件系统的更改。也没有现成的替代方案,这需要一个自定义文件系统过滤驱动程序来监听驱动程序请求。就像 SysInternals 的 ProcMon 实用程序使用的那种。我不知道有这样的驱动程序可以在 C# 程序中使用,您也不能用 C# 编写它们。这并不是一个常见的要求。
This is quite normal. Updating an existing file is quite dangerous since it can cause irretrievable data loss. A disk error (like disk full) while writing is very bad news. The common algorithm used:
Clearly this doesn't cause a Changed event to be raised, no file was changed.
Sorry, I didn't read the question well enough. There is no notification whatsoever for an app just opening a file for reading. FSW can only detect changes to the file system. There is no ready alternative either, this requires a custom file system filter driver that snoops on driver requests. Like the kind that SysInternals' ProcMon utility uses. I'm not aware of such a driver ready for use in a C# program, you can't write them in C# either. This just isn't a common requirement.