在VB.NET中读取时,从文件中排除二进制数据的前“ x”个字节

时间:2018-09-01 11:57:38

标签: vb.net binary-data

我是一个处理二进制文件的菜鸟。及其对这个问题的扩展:Reading 'x' bytes of data

我的二进制文件大小(如果 1025 KB ),即 1049600字节,其中包含 1024字节标头信息。我只想在 1023th 位(等于 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 个答案:

答案 0 :(得分:0)

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

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

在c#中创建一个大小为11的数组:

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之类的东西。