C# 我可以简单地“读取”正在使用的文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/203837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I simply 'read' a file that is in use?
提问by naspinski
I am trying to use a StreamReader to read a file, but it is always in use by another process so I get this error:
我正在尝试使用 StreamReader 读取文件,但它始终被另一个进程使用,因此出现此错误:
The process cannot access the file '\arfjwknasgmed17\C$\FLAG CONDITION\CP-ARFJN-FLAG.XLS' because it is being used by another process.
该进程无法访问文件“\arfjwknasgmed17\C$\FLAG CONDITION\CP-ARFJN-FLAG.XLS”,因为它正被另一个进程使用。
Is there a way I can read this without copying it? Or is that my only option?
有没有一种方法可以让我在不复制的情况下阅读它?或者这是我唯一的选择?
采纳答案by Brian R. Bondy
You can read the file only if the program that opened the file first specified read sharing rights on that file.
仅当打开文件的程序首先指定对该文件的读取共享权限时,您才能读取该文件。
If the file does indeed have no read sharing rights though, you wouldn't be able to copy it in the first place.
如果该文件确实没有读取共享权限,您将无法首先复制它。
You may not be able to access a file if you are specifying a sharing right that conflicts with the sharing right of a program that already has the file opened. For example you can't grant write access if the program that already has it opened isn't granting write access.
如果您指定的共享权限与已打开文件的程序的共享权限冲突,则您可能无法访问该文件。例如,如果已打开它的程序未授予写访问权限,则您无法授予写访问权限。
If the program that opened the file in the first place supports Volume Shadow Copy (VSS), you can also use VSS to gain access to the file.
如果首先打开文件的程序支持卷影复制 (VSS),您也可以使用 VSS 来访问文件。
There are commercial software drivers that allow you to access such files, even when they are in use. You used to be able to get Open File Manager by St-Bernards, and you can also use File Access Manager (FAM)by VisionWorks Solutions Inc. These drivers are typically OEM'ed to backup software companies for inclusion in their products.
有一些商业软件驱动程序允许您访问此类文件,即使它们正在使用中。您过去可以使用 St-Bernards 提供的 Open File Manager,您还可以使用VisionWorks Solutions Inc 提供的文件访问管理器 (FAM)。这些驱动程序通常由 OEM 提供给备份软件公司以包含在他们的产品中。
VSS works by telling the program that has the file opened already that another program would like to read from the file. VSS then does a copy of the file and lets you read from this copy. VSS does not work for legacy applications.
VSS 通过告诉已经打开文件的程序另一个程序想要从文件中读取来工作。然后 VSS 会复制该文件并让您从该副本中读取。VSS 不适用于遗留应用程序。
FAM transparently works for legacy and non-legacy programs alike by specifying an 'allowed list' of applications that can access exclusively opened and locked files. Only programs in this list are allowed access to these files. When a file is being opened, it goes into cache mode so that you will obtain a copy of the file as it was when the 'backup/open' of the file started. At this point the program that originally opened the file sees the file as it actually is, and the second program in the allowed list, sees the file as it was when the 'open/backup' of the file happened. This ensures consistency of the file.
FAM 通过指定可以访问独占打开和锁定文件的应用程序的“允许列表”,透明地为遗留和非遗留程序工作。只有此列表中的程序才能访问这些文件。当一个文件被打开时,它会进入缓存模式,这样你就可以获得文件的副本,就像文件的“备份/打开”开始时一样。此时,最初打开文件的程序会看到文件的实际情况,而允许列表中的第二个程序会看到文件“打开/备份”发生时的情况。这确保了文件的一致性。
回答by JST
try the below code.
试试下面的代码。
FileStream fileStr = File.Open(<full file name>, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
fileStream = new StreamReader(fileStr);
I have tried it on Windows XP. If the file is already open in write mode by some other process & it has not specified sharing rights, you will still be able to open the file in read mode.
我已经在 Windows XP 上试过了。如果文件已经被其他进程以写入模式打开并且它没有指定共享权限,你仍然可以以读取模式打开文件。
Disclaimer: It works, but then, I am not sure if you should use it in production code. I am not yet able to find any formal documentation that says it should work.
免责声明:它有效,但是,我不确定您是否应该在生产代码中使用它。我还没有找到任何说明它应该有效的正式文件。