如何使用 .net StreamReader 打开已打开的文件? [英] How do I open an already opened file with a .net StreamReader?

查看:26
本文介绍了如何使用 .net StreamReader 打开已打开的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 .csv 文件,我将它们用作测试平台的一部分.我可以打开它们并毫无问题地读取它们除非我已经在 Excel 中打开了文件,在这种情况下我会收到 IOException:

I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException:

System.IO.IOException:进程无法访问文件TestData.csv",因为它正被另一个进程使用.

System.IO.IOException : The process cannot access the file 'TestData.csv' because it is being used by another process.

这是测试台上的一个片段:

This is a snippet from the test bench:

using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false))
{
    // Process the file
}

这是 StreamReader 的限制吗?我可以在其他应用程序(例如 Notepad++)中打开该文件,因此它不会是 O/S 问题.也许我需要使用其他课程?如果有人知道我如何解决这个问题(除了关闭 excel!),我将不胜感激.

Is this a limitation of StreamReader? I can open the file in other applications (Notepad++ for example) so it can't be an O/S problem. Maybe I need to use some other class? If anyone knows how I can get round this (aside from closing excel!) I'd be very grateful.

推荐答案

正如 Jared 所说,除非打开文件的其他实体允许共享读取,否则您不能这样做.Excel 允许共享读取,即使对于它已打开用于写入的文件也是如此.因此,您必须使用 FileShare.ReadWrite 参数打开文件流.

As Jared says, You cannot do this unless the other entity which has the file open allows for shared reads. Excel allows shared reads, even for files it has open for writing. Therefore, you must open the filestream with the FileShare.ReadWrite parameter.

FileShare 参数经常被误解.它指示文件的其他 打开器可以做什么.它适用于过去和未来的开场白.将 FileShare 视为对先前打开程序(例如 Excel)的追溯性禁止,而是当前 Open 或任何未来 Opens 不得违反的约束.

The FileShare param is often misunderstood. It indicates what other openers of the file can do. It applies to past as well as future openers. Think of FileShare not as a retroactive prohibition on prior openers (eg Excel), but a constraint that must not be violated with the current Open or any future Opens.

在当前尝试打开文件的情况下,FileShare.Read 表示仅当任何先前的打开者已为读取打开此文件时,才能成功为我打开此文件."如果您在 Excel 为写入而打开的文件上指定 FileShare.Read,您的打开将失败,因为它会违反约束,因为 Excel 已为写入而打开.

In the case of the current attempt to open a file, FileShare.Read says "open this file for me successfully only if it any prior openers have opened it only for Read." If you specify FileShare.Read on a file that is open for writing by Excel, your open will fail, as it would violate the constraint, because Excel has it open for writing.

因为 Excel 已打开文件进行写入,如果您希望您的成功打开,您必须使用 FileShare.ReadWrite 打开该文件.考虑 FileShare 参数的另一种方式:它指定其他人的文件访问权限".

Because Excel has the file open for writing, you must open the file with FileShare.ReadWrite if you want your open to succeed. Another way to think of the FileShare param: it specifies "the other guy's file access".

现在假设一个不同的场景,您正在打开一个当前未被任何其他应用程序打开的文件.FileShare.Read 说未来的开启者只能以读取权限打开文件".

Now suppose a different scenario, in which you're opening a file that isn't currently opened by any other app. FileShare.Read says "future openers can open the file only with Read access".

从逻辑上讲,这些语义是有道理的 - FileShare.Read 的意思是,如果其他人已经在写文件,你就不想读它,如果你已经在写文件,你也不希望其他人写文件阅读它.FileShare.ReadWrite 意味着,即使另一个人正在编写文件,您也愿意阅读该文件,并且在您阅读文件时让另一个打开者写入文件也没有问题.

Logically, these semantics make sense - FileShare.Read means, you don't want to read the file if the other guy is already writing it, and you don't want the other guy to write the file if you are already reading it. FileShare.ReadWrite means, you are willing to read the file even if the another guy is writing it, and you have no problem letting another opener write the file while you are reading it.

在任何情况下,这都不允许多个作者.FileShare 类似于数据库 IsolationLevel.您在此处所需的设置取决于您需要的一致性"保证.

In no case does this permit multiple writers. FileShare is similar to a database IsolationLevel. Your desired setting here depends on the "consistency" guarantees you require.

示例:

using (Stream s = new FileStream(fullFilePath, 
                                 FileMode.Open,
                                 FileAccess.Read,
                                 FileShare.ReadWrite))
{
  ...
}

或者,

using (Stream s = System.IO.File.Open(fullFilePath, 
                                      FileMode.Open, 
                                      FileAccess.Read, 
                                      FileShare.ReadWrite))
{
}

<小时>

附录:

关于 System.IO.FileShare 的文档有点苗条.如果您想了解直接的事实,请转至 文档Win32 CreateFile 函数,更好地解释了 FileShare 的概念.

The documentation on System.IO.FileShare is a little slim. If you want to get the straight facts, go to the documentation for the Win32 CreateFile function, which explains the FileShare concept better.

这篇关于如何使用 .net StreamReader 打开已打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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