检测 C# 中的文件读取 [英] Detect File Read in C#

查看:19
本文介绍了检测 C# 中的文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FileSystemWatcher 检查文件何时被修改或删除,但我想知道是否有任何方法可以检查文件何时被另一个应用程序读取.

I'm using FileSystemWatcher to check when a file is modified or deleted, but I'm wondering if there is any way to check when a file is read by another application.

示例:我的硬盘上有文件 C: est.txt 并且正在使用 FileSystemWatcher 观看它.另一个程序(不在我的控制之下)去读取那个文件;我想捕获该事件,如果可能,检查正在读取文件的程序,然后相应地修改文件的内容.

Example: I have the file C: est.txt on my harddrive and am watching it using FileSystemWatcher. Another program (not under my control) goes to read that file; I would like to catch that event and, if possible, check what program is reading the file then modify the contents of the file accordingly.

推荐答案

听起来你想在外部读取日志文件时写入日志文件,或者类似的东西.如果是这种情况,则有一个 NotifyFilters 值 LastAccess.确保将其设置为 FileSystemWatcher.NotifyFilter 属性中的标志之一.上次访问时间的更改将触发 FileSystemWatcher 上的 Changed 事件.

It sounds like you want to write to your log file when your log file is read externally, or something to that effect. If that is the case, there is a NotifyFilters value, LastAccess. Make sure this is set as one of the flags in your FileSystemWatcher.NotifyFilter property. A change in the last access time will then fire the Changed event on FileSystemWatcher.

目前,FileSystemWatcher 不允许您直接区分读取和更改;它们都基于对 LastAccess 的更改"触发 Changed 事件.因此,监视对大量文件的读取是不可行的.但是,您似乎知道您正在观看哪个文件,因此如果您有该文件的 FileInfo 对象,并且 FileSystemWatcher 触发了它的 Changed 事件,您可以获得一个新的并比较 LastAccessTime 值.如果访问时间发生变化,而 LastWriteTime 没有发生变化,则您的文件只会被读取.

Currently, FileSystemWatcher does not allow you to directly differentiate between a read and a change; they both fire the Changed event based on the "change" to LastAccess. So, it would be infeasible to watch for reads to a large number of files. However, you seem to know which file you're watching, so if you had a FileInfo object for that file, and FileSystemWatcher fired its Changed event, you could get a new one and compare LastAccessTime values. If the access time changed, and LastWriteTime didn't, your file is only being read.

现在,简单来说,您在读取文件时对文件所做的更改不会立即显示在其他应用程序中,您也无法先到达那里"、锁定文件并在他们看到之前写下来.因此,您不能使用 FileSystemWatcher 来拦截"读取请求并显示您希望该应用程序看到的内容.另一个应用程序的用户可以看到您刚刚编写的内容的唯一方法是该应用程序是否也在监视该文件并重新加载该文件.这将触发另一个 Changed 事件,只要其他应用程序继续重新加载文件,就会导致无限循环.

Now, in simplest terms, changes you make to the file while it is being read are not going to immediately show up in the other app, nor are you going to be able to "get there first", lock the file and write to it before they see it. So, you cannot use FileSystemWatcher to "intercept" a read request and show the content you want that app to see. The only way the user of another application can see what you just wrote is if the application is also watching the file and re-loads the file. That will fire another Changed event, causing an infinite loop as long as the other application continues to reload the file.

您还将获得一个用于读取和写入的 Changed 事件.在文本编辑器中打开一个文件(几乎任何人都可以),进行一些更改,然后如果您正在查找对上次访问时间的更改,则保存将触发两个 Changed 事件.当编辑器打开文件时,第一个将关闭;那时,您可能无法判断会发生写入,因此,如果您正在寻找对文件的纯只读访问,那么您就是 SOL.

You will also get a Changed event for a read and a write. Opening a file in a text editor (virtually any will do), making some changes, then saving will fire two Changed events if you're looking for changes to Last Access Time. The first one will go off when the file is opened by the editor; at that time, you may not be able to tell that a write will happen, so if you are looking for pure read-only accesses to the file then you're SOL.

这篇关于检测 C# 中的文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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