我如何打开与.NET的StreamReader已打开的文件?文件、NET、StreamReader

2023-09-02 01:22:22 作者:堇墨浮华

我有我使用的是作为一个测试平台的一部分,一些的.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的限制?我可以打开在其它应用程序的文件(记事本++例如),所以它不能是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.

推荐答案

由于贾里德说,你不能这样做,除非它打开了该文件的其它实体允许共享的读取。 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.

文件共享参数常常被误解。它表示什么的等的文件叹为观止可以做。它适用于过去和未来叹为观止。认为文件共享的而不作为对现有开放剂追溯禁止(例如Excel),但打开一个约束不能与当前打开或任何未来的侵犯。

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文件的您的开放获得成功。另一种方式去思考的文件共享参数的:它指定其他人的文件访问。

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.

在任何情况下,这是否允许多个作家。文件共享是类似于数据库的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函数的文档,这也解释了文件共享的概念更好。

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.