【发布时间】:2017-03-14 19:15:59
【问题描述】:
File.ReadAllBytes 在两次调用之间没有足够的间隔时会导致 IOException 吗?
当我设置网格的 Row 和 Col 时,它会触发 RowColChange 事件。 RowColChange 有一些代码使用 File.ReadAllBytes 打开同一个文件。 我了解 ReadAllBytes 在内部使用 on FileStream ,因此文件流在使用后关闭。但是在告诉操作系统文件已释放时是否可能会有一些延迟 因此 File.ReadAllBytes 的后续使用可能会失败并引发异常。 有什么想法吗?谢谢!
grid.Row = 0
grid.Row = 1
grid.Col = 3
Private Sub grid_RowColChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles grid.RowColChange
'Is it possible to get IOException saying the process can't access the file because it is being used by another process.
Display(File.ReadAllBytes(filePath))
End Sub
【问题讨论】:
-
reference source 建议“否”:
ReadAllBytes使用FileShare.Read打开文件,记录为“允许随后打开文件以供读取”。你确定没有并发写入吗? -
网上有很多关于如何做到这一点的例子以及
stackoverflowstackoverflow.com/questions/35307459/… -
@dlatikay,根本没有写入文件。感谢您阐明 ReadAllBytes 的用法。由于我没有证据表明该文件正在被另一个进程使用,因此我只是试图消除一些情况。
-
@dlatikay 尽管“//Do a blocking read”注释在 InternalReadAllBytes 中是什么意思?
标签: .net vb.net system.io.file