确定文件是否打开 [英] Determine if a file is open

查看:119
本文介绍了确定文件是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来确定文件是否被任何包含不锁定文件的应用程序(如记事本)所打开。



我需要检测何时给定的文件myfile.txt不再打开包括记事本在内的任何应用程序 - 所以我不能使用File.Open(...)独占访问测试,因为文件没有锁定。

解决方案

没有。当记事本打开文件时,它已经在中读取了整个文件,然后关闭它。因此,操作系统中没有任何痕迹将记事本的私有内存与磁盘上的文件链接在一起。



独占打开文件不起作用,因为记事本没有打开文件。搜索记事本的句柄表将不起作用,因为记事本没有打开文件。

检测这个的唯一方法是编写一个注入每个进程的非托管DLL扫描他们的虚拟内存,搜索确切的文件内容。不推荐。


Is there any way to determine if a file is open by anything include applications that do not lock the file (like notepad).

I need to detect when a given file myfile.txt is no longer open in any application including notepad - so i cannot use File.Open(...) with exclusive access to test since the file has no lock on it.

解决方案

No. When Notepad has opened a file, it has read the entire file in and then closed it. So there is no trace in the OS that links Notepad's private memory with the file on disk.

Opening the file exclusively will not work, because Notepad does not have the file open. Searching Notepad's handle table will not work, because Notepad does not have the file open.

The only way to detect this is to write an unmanaged DLL that is injected into every process to scan their virtual memory, searching for the exact file contents. Not recommended.

这篇关于确定文件是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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