在过去,我们会使用下列写法来读取二进制文件:

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)

 

 

相关文章:

  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2021-12-02
猜你喜欢
  • 2021-04-08
  • 2021-12-13
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案