简体   繁体   English

如何从正在使用的文件中读取

[英]How to read from a file that is in use

There's a file I wanted to get into, but whenever I try to open it I get the message "The process cannot access the file because it is being used by another process". 我想进入一个文件,但是每当我尝试打开它时,都会收到消息“该进程无法访问该文件,因为该文件正在被另一个进程使用”。

Well, I want in! 好吧,我要进去! So, how can i do it? 那么,我该怎么办呢?

I've been brainstorming a few ways to try, I'm hoping to get some input on other ways, or if my ideas wouldn't work for some reason that is not apparent to me. 我一直在集思广益尝试一些方法,我希望从其他方法上得到一些建议,或者如果我的想法由于某种我认为不明显的原因而行不通。

  • Idea 1 The folder knows where the file is, it just won't open it. 想法1文件夹知道文件在哪里,只是不会打开它。 What if I create a program to read from the memory address of the file, copy it, then rebuild it somewhere else? 如果我创建了一个程序来读取文件的内存地址,然后将其复制,然后在其他地方重新构建,该怎么办? I'm not sure if this has hope, because it relies on the file being the issue. 我不确定这是否有希望,因为它取决于问题所在。
  • Idea 2 How does my process know that another process is using the file? 想法2我的进程如何知道另一个进程正在使用该文件? If it's checking against all the other processes, maybe I can also figure out which process is using that file and pause it or end it. 如果要对照所有其他进程进行检查,也许我还可以找出哪个进程正在使用该文件并将其暂停或结束。

Either of these ideas will probably take me weeks. 这些想法中的任何一个都可能需要我几个星期。 Is anyone more creative and can think of another way; 是否有人更有创造力,可以想到另一种方式? or more knowledgeable and eliminate an impractical idea? 或更精通知识并消除不切实际的想法?

In Windows, applications are allowed to obtain exclusive locks on files . 在Windows中,允许应用程序获取文件的排他 When the process opens the file , one thing you specify is who else can access it while your process does (those are the .NET methods, but equivalents exist in other languages). 当进程打开文件时 ,您指定的一件事是在进程执行过程中还有谁可以访问它 (这些是.NET方法,但其他语言中也存在等效方法)。 Excel, for example, is notorious for getting an exclusive lock when you open a file. 例如,Excel在打开文件时因获得排他锁而臭名昭著。 The way around it is usually to find the offending process and kill it to break the lock. 解决它的方法通常是找到有问题的进程并杀死它以打破锁定。 Unlocker is the app that I'm most familiar with to accomplish this. Unlocker是我最熟悉的应用程序。 If the process is a System process, however, you may not be able to kill it. 但是,如果该进程是系统进程,则可能无法将其杀死。 You'd have to reboot to reset the lock. 您必须重新启动才能重置锁定。

Reading directly from another process's memory is unlikely to be reliable. 直接从另一个进程的内存中读取数据不太可能可靠。 The application may not have an in-memory copy, may not have a complete in memory copy, may not have a consistent in memory copy, and may not have an in memory copy that matches what's on disk (If they're editing the document, for example). 该应用程序可能没有内存中的副本,可能没有完整的内存中的副本,可能没有一致的内存中的副本,并且可能没有与磁盘上的内容匹配的内存中副本(如果他们正在编辑文档) , 例如)。

Your process knows that the file is locked because when it tries to open the file, it does so by asking the operating system for access to the file. 您的进程知道文件已锁定,因为它在尝试打开文件时会通过要求操作系统访问文件来进行锁定。 The operating system responds saying, "Request denied. Another process has this file open and locked." 操作系统响应说:“请求被拒绝。另一个进程打开并锁定了此文件。” The OS doesn't tell your process what process has the file open because trying to open a file doesn't include asking for who already has it open. 操作系统不会告诉您进程哪个文件已打开,因为尝试打开文件不包括询问谁已经打开了文件。 Your process must ask the right question to get the answer you're looking for. 您的过程必须提出正确的问题才能获得所需的答案。

Windows makes you specify a sharing modes when opening a file. Windows使您可以在打开文件时指定共享模式。 The sharing mode may prevent the file from being read, written, or deleted while you have it open. 共享模式可能会阻止您在打开文件时读取,写入或删除文件。 If you want to allow simultaneous read access you should include FILE_SHARE_READ in the dwShareMode parameter when you call CreateFile ( http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx ). 如果要允许同时读取访问,则在调用CreateFilehttp://msdn.microsoft.com/zh-cn/library/windows/desktop/aa363858 ( dwShareMode时,应在dwShareMode参数中包含FILE_SHARE_READ 。 aspx )。

In other words, if you want to enable concurrent access to an open file you must modify the way the file is opened in the first place. 换句话说,如果要启用对打开文件的并发访问,则必须首先修改文件的打开方式。

The portable standard libraries in C and Java don't offer a way to set the sharing mode when opening a file, but their usual implementations on windows set the sharing mode to READ+WRITE. C和Java中的可移植标准库没有提供一种在打开文件时设置共享模式的方法,但是它们在Windows上的常规实现将共享模式设置为READ + WRITE。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM