【问题标题】:BufferedStream - an attempt was made to move the file pointer before the beginning of the fileBufferedStream - 尝试将文件指针移动到文件开头之前
【发布时间】:2016-09-13 17:31:55
【问题描述】:

我正在打开一个非常小的文件(大约 200 kb),然后我在 BufferedStream 中打开它,因为我要多次阅读它。当我尝试创建 BufferedStream 时,我得到一个 IOException:

var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true);
using (var bfs = new BufferedStream(stream)); // IOException: An attempt was made to move the file pointer before the beginning of the file.

什么可能导致 IO 异常?我的应用正在使用 .NET 4.6

【问题讨论】:

    标签: c# file exception io .net-4.6


    【解决方案1】:

    这并没有具体回答您的问题,但您不需要使用BufferedStream。缓冲已经融入FileStream

    https://blogs.msdn.microsoft.com/brada/2004/04/15/filestream-and-bufferedstream/

    特别是以下引语:

    答案(来自 System.IO 命名空间的开发者 Brian Grunkemeyer):

    不,将 BufferedStream 包装在 FileStream 周围的好处为零。大约 4 年前,我们将 BufferedStream 的缓冲逻辑复制到 FileStream 中,以鼓励更好的默认性能(成功坑)。事实上,我认为 .NET Framework 中没有任何 Streams 需要它,但如果自定义 Stream 实现默认不做缓冲,则可能需要它。

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 2019-12-09
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多