简体   繁体   English

HOWTO:监控Windows中其他进程的文件访问?

[英]HOWTO: Monitoring file access by another process in Windows?

I am working on a program that processes image files. 我正在处理一个处理图像文件的程序。 The problem I am running into is some of the images are very large and I don't know when the copy process is done. 我遇到的问题是一些图像非常大,我不知道复制过程何时完成。 Right now the code is written in C# and is using the FileSystemWatcher class to monitor the files. 现在代码是用C#编写的,并且使用FileSystemWatcher类来监视文件。 The only problem is there is no event for when another process is DONE with the file. 唯一的问题是当另一个进程与文件完成时没有事件。

The current logic is to watch for created and changed events and than wait 5 seconds in hopes that by that point in time the copy process is done. 当前的逻辑是观察创建和更改的事件,而不是等待5秒,希望在那个时间点完成复制过程。 While this works on my development machine, unfortunately customers report issues with the whole process. 虽然这适用于我的开发机器,但不幸的是客户报告整个过程的问题。

Years ago when I wrote this code I did see that at a lower level, I think it was part of the DDK SDK or something, it is possible to find out a lot more information about how another program is accessing a file. 多年前,当我编写这段代码时,我确实发现在较低级别,我认为它是DDK SDK的一部分,可以找到有关另一个程序如何访问文件的更多信息。

Can anyone speak into this and point me in the right direction? 谁能说到这一点并指出我正确的方向? I do expect it to be C++ code, not C# code. 我确实希望它是C ++代码,而不是C#代码。


To answer some questions below: 要回答以下问题:

The issue is that OTHER processes are copy images into directories that my program is monitoring. 问题是其他进程是复制映像到我的程序正在监视的目录中。 Once the images are fully copied my program needs to start processing the images as soon as possible. 一旦图像被完全复制,我的程序需要尽快开始处理图像。 The issue is that many images, at times hundreds of images, will be copied at one time. 问题在于,一次会复制许多图像,有时会有数百张图像。 Since each image needs to be processed as fast as possible, the system needs to know as soon as possible that the image is fully copied and can be processed. 由于每个图像都需要尽快处理,因此系统需要尽快知道图像是否已完全复制并可以处理。

I am fully aware there is NO way to do what I want to do via the WinSDK, but I am 99.9% sure it IS possible via the WinDDK, maybe I have the name wrong, I Googled that and got "Windows Driver Kit (WDK)". 我完全清楚没有办法通过WinSDK做我想做的事情,但我99.9%肯定可以通过WinDDK,也许我有错误的名字,我用Google搜索并获得“Windows Driver Kit(WDK) )”。 I am pretty sure the way SysInternals pulls off all their cool tricks is by using WDK level functions. 我很确定SysInternals通过使用WDK级别函数来解决所有酷炫技巧的方式。

What you might be interested in is sys internals procmon tool. 您可能感兴趣的是sys internals procmon工具。 Among other functionality it monitors file access giving you a lot of details. 在其他功能中,它监视文件访问,为您提供大量详细信息。 Unfortunately sources are not available but here's a thread about previous version (filemon) started by somebody with an issue similar to yours. 不幸的是,源代码不可用,但这里有一个关于以前版本 (filemon) 的帖子,由某人开始,问题类似于你的。

Also you may want to look into open source alternative , it may have the code you are looking for. 此外,您可能希望查看开源替代方案 ,它可能包含您正在寻找的代码。

Alternatively you can use ProcMon as a monitor tool and parse its output. 或者,您可以使用ProcMon作为监视工具并解析其输出。 Using built in filter functionality you'll be able to limit your log file entries to specific folder and type/data of file access event. 使用内置过滤器功能,您将能够将日志文件条目限制为特定文件夹和文件访问事件的类型/数据。


[ Update ] [ 更新 ]

Recently I had a chance to revisit this issue for one of my projects, what I found is that call 最近我有机会为我的一个项目重新审视这个问题,我发现的是那个电话

File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)

will throw IOException if file is still used by another process. 如果另一个进程仍在使用该文件,则将抛出IOException。

So what you can do is after you get Created notification from FileSystemWatcher call File.Open every couple of seconds, once you stop getting exception you know that another process is done with the file. 所以你可以做的是在你从FileSystemWatcher调用File.Open每隔几秒钟获得一次创建通知后,一旦你停止获得异常就知道另一个进程已经完成了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 监视Windows中的应用程序文件访问 - monitoring applications file access in Windows 如何访问Windows中另一个进程正在使用的文件? - How to access a file that is being used by another process in Windows? 该进程无法访问该文件,因为该文件正在被另一个进程使用。” Windows应用程序开发 - The process cannot access the file because it is being used by another process” Windows Application Development Windows 8.1应用程序-该进程无法访问文件,因为该文件正在被另一个进程使用 - Windows 8.1 app - The process cannot access the file because it is being used by another process 该进程无法访问另一个进程使用的文件 - The process cannot access the file used by another process 监控文件访问 FileSystemWatcher - Monitoring file access FileSystemWatcher Windows 服务文件流给出 System.IO.IOException:进程无法访问文件“文件名”,因为它正被另一个进程使用 - Windows Service Filestream giving System.IO.IOException: The process cannot access the file "filename" because it is being used by another process 该进程无法访问文件'C:\\ PCLtoMove \\ print.pcl',因为它正在被另一个进程使用。 Windows服务 - The process cannot access the file 'C:\PCLtoMove\print.pcl' because it is being used by another process. Windows service Windows服务中的另一个进程正在使用的文件 - File being used by another process in windows service 监视Windows系统按进程进行读/写操作? - Monitoring Windows system Read/Write operations by process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM