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

查看:161
本文介绍了检测文件读取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:\test.txt在我的硬盘,并使用FileSystemWatcher的我看着它。另一个程序(不是我的控制之下)去读取文件;我想赶上该事件,如果可能,检查什么程序读取然后将文件修改相应的文件的内容。

Example: I have the file C:\test.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事件的读取和写。打开文件在文本编辑器(几乎所有的都行),做一些改变,然后保存会令两部更改的事件,如果你正在寻找改变最后访问时间。当该文件被编辑器中打开第一个将熄灭;在那个时候,你可能无法告诉一个写会发生,因此,如果您正在寻找纯粹的只读访问该文件,那么你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天全站免登陆