读取 VB.NET 时从文件中排除二进制数据的前"x"字节



我是一个处理二进制文件的菜鸟。它是这个问题的扩展:读取"x"字节的数据

我的二进制文件大小为 1025 KB,即1049600字节,包含1024 字节的标头信息。我想只在第1023位(等于1048576 个字节(之后读取剩余数据。

如何排除前1024 个字节

我使用相同的代码,但我无法让它工作,我的代码有什么问题吗?

Dim arraySizeMinusOne = 5
Dim buffer() As Byte = New Byte(arraySizeMinusOne) {}
Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
fs.Read(buffer, 0, buffer.Length)
Dim _arraySizeMinusOne = 1048575
Dim _buffer() As Byte = New Byte(_arraySizeMinusOne) {}
'Process 1048576 bytes of data here
End Using

创建一个长度为整个文件减去、排除大小和负 1 的byte数组。最后一个1是因为 vb 数组与 c# 数组不同。在 vb 中:

Dim buff() As Byte = New Byte(10) {}

创建一个 11 大小的数组,并在 C# 中:

byte[] buff = new byte[10];

创建 10 大小!

Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
Dim buff() As Byte = New Byte(CInt(fs.Length - 1024 - 1)) {}
Dim buffTotal() As Byte = New Byte(CInt(fs.Length - 1)) {}
'read all file
fs.Read(buffTotal, 0, buffTotal.Length)
If fs.Length > 1024 Then
'move from 1024 byte to the end to buff array
Buffer.BlockCopy(buffTotal, 1024, buff, 0, buffTotal.Length - 1024)
End If
End Using

buffer不是一个好名字,因为在 vb 中已经存在一个类Buffer!将其更改为其他类似buff.

最新更新