Visual Basic 2005 - 读取二进制文件的演进过程
在过去,我们会使用下列写法来读取二进制文件:
Dim fs As FileStream = New FileStream(ImageFilePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim photo() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()
自从 Visual Basic 2005 提供了 Using...End Using 之后,我们会改写如下:
Dim photo As Byte()
Using fs As FileStream = New FileStream(ImageFilePath, FileMode.Open, FileAccess.Read)
Using br As BinaryReader = New BinaryReader(fs)
photo = br.ReadBytes(fs.Length)
End Using
End Using
但是别忘了,Visual Basic 2005 还提供了一个更便利的 My,所以,最简易的写法应该如下喔:
Dim photo As Byte()
photo = _
My.Computer.FileSystem.ReadAllBytes(ImageFilePath)