Vb.net 读/写二进制文件溢出

Vb.net 读/写二进制文件溢出,vb.net,Vb.net,我需要一些代码的帮助,请。这是我的密码: Public Function ReadBinaryFileLarge(strFilename As String) As Byte() Dim position As Integer = 0 Dim bufferSize As Integer = 4096 Dim bytes() As Byte Using fsOpen As FileStream = New FileStream(strFilename, F

我需要一些代码的帮助,请。这是我的密码:

    Public Function ReadBinaryFileLarge(strFilename As String) As Byte()
    Dim position As Integer = 0
    Dim bufferSize As Integer = 4096
    Dim bytes() As Byte

    Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open)
        ReDim bytes((fsOpen.Length) - 1)
        Do
            If (position + bufferSize) > fsOpen.Length Then
                fsOpen.Read(bytes, position, fsOpen.Length - position)
                Exit Do
            Else
                fsOpen.Read(bytes, position, bufferSize)
            End If

            position += bufferSize

            Application.DoEvents()
        Loop
    End Using

    Return bytes

End Function

Public Sub SaveBinaryFileLarge(strFilename As String, bytesToWrite() As Byte)
    Dim position As Integer = 0

    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
        Do
            Dim intToCopy As Integer = Math.Min(4096, bytesToWrite.Length - position)
            Dim buffer(intToCopy - 1) As Byte
            Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
            fsNew.Write(buffer, 0, buffer.Length)

            position += intToCopy

            Application.DoEvents()

        Loop While position < bytesToWrite.Length

    End Using

End Sub
公共函数ReadBinaryFileLarge(strFilename作为字符串)作为字节()
Dim位置为整数=0
Dim bufferSize为整数=4096
Dim bytes()作为字节
使用fsopenasfilestream=newfilestream(strFilename,FileMode.Open)
重拨字节数((fsOpen.Length)-1)
做
如果(位置+缓冲区大小)>fsOpen.Length,则
读取(字节、位置、fsOpen.Length-位置)
退出Do
其他的
读取(字节、位置、缓冲区大小)
如果结束
位置+=缓冲区大小
Application.DoEvents()
环
终端使用
返回字节
端函数
Public Sub SaveBinaryFileLarge(strFilename作为字符串,bytesToWrite()作为字节)
Dim位置为整数=0
使用fsNew作为FileStream=newfilestream(strFilename,FileMode.Create,FileAccess.Write)
做
Dim intToCopy As Integer=Math.Min(4096,bytesToWrite.Length-position)
Dim缓冲区(intToCopy-1)作为字节
Copy(bytesToWrite、position、buffer、0、intToCopy)
fsNew.Write(缓冲区,0,缓冲区长度)
位置+=内部复制
Application.DoEvents()
位置
我的问题是,对于大文件,字节数组不能声明为那么大。我需要将其加载到字节数组中,以便对字节数组执行一些加密工作。我正在使用以下代码:

    Public Function ReadBinaryFileTest(strFilename As String) As Byte()()
    Const SIZE As Integer = &H1000 '4096 <-experiment with this value
    Dim bytes()() As Byte

    Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read, SIZE)
        Dim ubound = (fsOpen.Length / SIZE) - 1
        bytes = new byte(ubound)()
        Dim read As Integer = 0

        For index As Integer = 0 To ubound
            bytes(index) = New Byte(SIZE - 1)
            read = fsOpen.Read(bytes(index), 0, SIZE)
            If read <> SIZE Then
                Array.Resize(bytes(index), read) 'this should only happen once if at all
            End If
        Next
    End Using

    Return bytes

End Function
公共函数ReadBinaryFileTest(strFilename作为字符串)作为Byte()()
Const SIZE As Integer=&H1000'4096您必须使用ReDim

ReDim bytes(ubound-1)

那么:bytes(index)=新字节(SIZE-1)Put{}-bytes(index)=新字节(SIZE-1){}
 bytes = New Byte(ubound)() {}