我可以简单地“阅读”吗? 正在使用的文件?

发布于 2024-07-06 12:49:22 字数 241 浏览 9 评论 0原文

我正在尝试使用 StreamReader 来读取文件,但它总是被另一个进程使用,所以我收到此错误:

该进程无法访问该文件 '\arfjwknasgmed17\C$\FLAG CONDITION\CP-ARFJN-FLAG.XLS' 因为 它正在被另一个进程使用。

有没有一种方法可以让我在不复制的情况下阅读它? 或者这是我唯一的选择?

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:

The process cannot access the file
'\arfjwknasgmed17\C$\FLAG
CONDITION\CP-ARFJN-FLAG.XLS' because
it is being used by another process.

Is there a way I can read this without copying it? Or is that my only option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-07-13 12:49:23

仅当打开该文件的程序首先指定该文件的读取共享权限时,您才可以读取该文件。

如果该文件确实没有读取共享权限,那么您一开始就无法复制它。

如果您指定的共享权限与已打开文件的程序的共享权限冲突,您可能无法访问该文件。 例如,如果已打开该程序的程序未授予写访问权限,则您无法授予写访问权限。

如果首先打开该文件的程序支持卷影复制 (VSS),您还可以使用 VSS 来访问该文件。

有一些商业软件驱动程序允许您访问此类文件,即使它们正在使用中也是如此。 您过去可以使用 St-Bernards 的 Open File Manager,也可以使用 文件访问管理器 (FAM) 由 VisionWorks Solutions Inc. 提供。这些驱动程序通常由备份软件公司 OEM 提供,以便包含在其产品中。

VSS 的工作原理是告诉已打开文件的程序另一个程序想要读取该文件。 然后,VSS 会复制该文件并让您从此副本中读取内容。 VSS 不适用于遗留应用程序。

FAM 通过指定可以访问以独占方式打开和锁定的文件的应用程序“允许列表”,透明地适用于旧版和非旧版程序。 仅允许此列表中的程序访问这些文件。 打开文件时,它会进入缓存模式,以便您获得文件“备份/打开”开始时的副本。 此时,最初打开该文件的程序会看到该文件的实际情况,而允许列表中的第二个程序会看到该文件发生“打开/备份”时的情况。 这确保了文件的一致性。

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.

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.

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.

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.

自演自醉 2024-07-13 12:49:23

尝试下面的代码。

FileStream fileStr = File.Open(<full file name>, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
fileStream = new StreamReader(fileStr);

我在Windows XP上试过了。 如果文件已被其他进程以写入模式打开& 它没有指定共享权限,您仍然可以以读取模式打开该文件。

免责声明:它有效,但是,我不确定您是否应该在生产代码中使用它。 我还没有找到任何正式文件表明它应该有效。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文