【问题标题】:StreamReader IO Exception Not Enough Space on DiskStreamReader IO 异常 磁盘空间不足
【发布时间】:2016-10-11 08:30:59
【问题描述】:

我正在尝试从网络驱动器中读取 160 个文本文件。我正在使用System.IO.StreamReader。出于某种原因,当它到达第 22 个文件时,它会抛出错误: IO 异常未处理磁盘空间不足

我的本​​地磁盘和网络驱动器都有足够的空间(这些是小文件),我不太明白为什么 StreamReader 会抛出这个异常(StreamWriter 会更有意义)。

这是我的一些代码:

'This sub is called 160 times (each file)
Private Sub readFile(filePath as String)
    'error gets thrown on this line
    Dim reader As System.IO.StreamReader = New System.IO.StreamReader(filePath)
    'Reading through this file...
    '...
    '...
    reader.Close()
End Sub

谢谢。

【问题讨论】:

  • 你知道创建 StreamReader 时是否出现异常吗?或者当你打电话时 Read ?另外,什么是异常类型? IOException 或更具体?此外,您可能希望使用 Using 块,因为它会在不再使用时自动处理资源...
  • 它发生在我创建 StreamReader 时。这是一个IOException。我会查找 Using 块 - 谢谢。
  • 刚试过Using。没有运气。不过还是谢谢。
  • 流分配必须释放的资源,而您不允许这样做,因为您不处置它们。任何具有 Dispose 方法的东西都应该在 Using 块中使用。
  • 不幸的是,即使在 Using 块中,它仍然不起作用。

标签: vb.net file text streamreader ioexception


【解决方案1】:

我决定尝试从本地目录中读取这些文件,而不是从网络驱动器中读取。

但是,当我去复制它们(通过 Windows)时,我收到错误:“磁盘空间不足”。这些文件总共有 40mb,我有数百 GB 的空闲空间。按“再试一次”20次后,它慢慢地复制越来越多,直到完成。奇怪的是,它会分批复制 42 个文件。

然后,我可以毫无问题地从本地目录中读取它们。看来问题与网络驱动器有关。

我的程序化解决方案是尝试复制它们,直到全部复制(通常需要大约一分钟)。

 'we start by copying all the files to a local drive. Since reading off the M: drive always throws lame exceptions...
 For Each f As String In My.Computer.FileSystem.GetFiles(fileDirectory)
     While (My.Computer.FileSystem.FileExists(toCopyTofileName)) = False
         Try
             My.Computer.FileSystem.CopyFile(f, toCopyTofileName)
         Catch ex As Exception
             Threading.Thread.Sleep(1000)
         End Try
     End While
 Next

目前,Threading.Thread.Sleep(1000) 很好(我只每秒重试一次),但我想我会将其切换为 Timer 或其他什么。我还应该添加一个最大超时限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2013-10-09
    • 2015-12-04
    • 2015-12-30
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多