为什么 binaryreader 会剥离我文件的第一个字节?

Why is binaryreader stripping first byte of my file?

所以我正在编写一个程序,它将从 vb.net 中的二进制文件接收设置。我一次读取 25 个字节。然而,当我检索我的字节数组时,它缺少第一个字节,而且只有第一个字节。

        Dim bytes(24) As Byte
        Using fs As BinaryReader = New BinaryReader(File.Open(folder.SelectedPath & "\*********", FileMode.Open, FileAccess.Read))
            While fs.Read() > 0
                fs.Read(bytes, 0, bytes.Length)
            End While
            fs.Close()
        End Using

我得到的数组将只丢失第一个字节,在我的例子中是 0x40。为什么会发生这种情况,我应该怎么做才能避免这种情况?

因为While fs.Read() > 0中的fs.Read从流中读取了一些东西,因此流不再位于位置0。

以下是您应该如何操作:

Dim bytes(24) As Byte
Using fs As BinaryReader = New BinaryReader(File.Open(folder.SelectedPath & "\*********", FileMode.Open, FileAccess.Read))

    Dim total_read As Integer

    While total_read < bytes.Length
        Dim read = fs.Read(bytes, total_read, bytes.Length - total_read)
        If read = 0 Then
            Exit While
        End If

        total_read += read
    End While
End Using