如何读取另一个进程已经使用的文件 [英] How To Read a File that Already used by another Process

查看:81
本文介绍了如何读取另一个进程已经使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helloo Everbody

我正在尝试为我的大学项目构建AV,并且在扫描过程中出现异常-访问被拒绝,文件被另一个进程使用,因此无法扫描该文件.我也使用Fileshare.ReadWrite但没有用. 大多数情况下使用C:驱动器文件.

Helloo Everbody

I am trying to build an AV for my college project and during scanning I am getting exception-Access denied,file is used by another process and hence can''t scan the file.I also used Fileshare.ReadWrite but of no use.It''mostly with C: drive files.

推荐答案

如果文件正在使用中,则进行另一个处理(假设它不是您的应用程序在保留它) -如果每个文件都包含此文件,则可能是)对该文件具有排他锁.在这种情况下,系统将不允许您打开文件.我所建议的最好的办法是将文件添加到困难"文件列表中,然后再试一次.该文件可能会同时变为免费.
If the file is in use, then the other process (assuming it isn''t your application that is holding it - if you get this with every file, then it may be) has an exclusive lock on the file. The system will not allow you to open the file under these circumstances. The best thing I can suggest is that you add the file to a list of "difficult" files, and try again later. The file may become free in the meantime.


如果您可以访问其他应用程序的源代码,则可以将其文件设置为以下内容:

If you have access to the source of the other application you can set it''s file open to something like:

FileStream writer = new FileStream("C:\theFile", FileMode.Open, FileAccess.Write, FileShare.Read);



然后,您可以使用以下文件打开文件:



Then you can open the file using:

FileStream reader = new FileStream("C:\theFile", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);



请注意,您始终可以尝试以只读模式打开文件,如果成功,还取决于文件的初始用户的状态.

如果这样做不适合您,则需要找出谁持有该文件.如果您知道,那您就离解决方案更近了一步.
您可以使用句柄 [ ^ ]查找该应用程序.

根据您发现的内容,您可以采取适当的措施;如果事实证明您自己的应用程序已经具有该文件的句柄,则您可能只是有多个线程在执行相同的操作……真的一样:).

干杯,AT



Note that you can always try to open the file in read only mode, if you succeed also depends on how nice the initial user of the file is.

If this doesn''t do it for you, you need to sort out who is holding the file. If you know that, you''re one step closer to the solution.
you can use Handle[^] to find the application.

Depending on what you find you can take the appropriate action; if it turns out that your own applicaiton already has a handle to the file you might be just have multiple threads doing the same... really the same :).

Cheers, AT




你可以试试看.

Hi,

You can try this.

FileStream fs = new FileStream(@"<file path>", FileMode.Open, FileAccess.Read, FileShare.Read);



因此< file path =">应该是您要访问的文件.

然后应该可以根据需要使用"fs"对象.

问候.



So <file path=""> should be file that you want to access.

then should be able to use "fs" object as you want.

Regards.


这篇关于如何读取另一个进程已经使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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