Jyi*_*ina 5 .net vb.net system.io.file
File.ReadAllBytes 在两次调用之间没有足够的调用间隔时会导致 IOException 吗?
当我设置网格的 Row 和 Col 时,它会触发 RowColChange 事件。RowColChange 有一些代码可以使用 File.ReadAllBytes 打开同一个文件。我知道 ReadAllBytes 内部在 FileStream 上使用 using 因此文件流在使用后关闭。但是在告诉操作系统文件被释放时是否可能有一些延迟,因此 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
Run Code Online (Sandbox Code Playgroud)
请尝试以下操作:
Using fileStream = New FileStream("path", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using streamReader = New StreamReader(fileStream)
Dim content = streamReader.ReadToEnd()
End Using
End Using
Run Code Online (Sandbox Code Playgroud)
实际上有可能两个线程从同一个文件读取同一个文件,请尝试使用上面的代码来读取该文件。
| 归档时间: |
|
| 查看次数: |
5899 次 |
| 最近记录: |