读/写二进制文件

时间:2012-01-30 03:10:06

标签: vb.net

我有这个代码运作良好:

Public Function LoadBinaryFile(strFilename As String) As Byte()
    Using fsSource As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read)
        ' Read the source file into a byte array.
        Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
        Dim numBytesToRead As Integer = CType(fsSource.Length, Integer)
        Dim numBytesRead As Integer = 0

        'tsProgressBar.Minimum = 0
        'tsProgressBar.Maximum = numBytesToRead

        While (numBytesToRead > 0)
            ' Read may return anything from 0 to numBytesToRead.
            Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
                numBytesToRead)
            ' Break when the end of the file is reached.
            If (n = 0) Then
                Exit While
            End If
            numBytesRead = (numBytesRead + n)
            numBytesToRead = (numBytesToRead - n)

            'tsProgressBar.Value = numBytesRead

        End While
        numBytesToRead = bytes.Length

        Return bytes

    End Using
End Function

我有这个代码来保存文件也很好用:

Public Function SaveBinaryFile(strFilename As String, bytesToWrite() As Byte) As Boolean
    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
        fsNew.Write(bytesToWrite, 0, bytesToWrite.Length)
    End Using
End Function

我所需要的是修改SaveBinaryFile函数以实现进度条的一些帮助。

决赛:

好的,我自己写了这个函数。这是:

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

    'frmMain.tsProgressBar.Value = 0

    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
            'frmMain.tsProgressBar.Value = ((position / fsOpen.Length) * 100)
            'frmMain.tsProgressBar.Refresh()
            Application.DoEvents()
            position += bufferSize
        Loop
    End Using

    Return bytes

End Function

3 个答案:

答案 0 :(得分:2)

My.Computer.Filesystem.ReadAllBytes(“filename”)将整个文件读入字节数组。

My.Computer.Filesystem.WriteAllBytes(“filename”,bytes,false)将其写回。

答案 1 :(得分:1)

我从未尝试过在FileStream上使用Async选项,但似乎没有任何事件处理程序。

我会把它分解成一个循环,你有要写入的总字节数,这样你就可以循环写入4k,更新进度条并继续循环。

Public Sub SaveBinaryFile(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)
            ProgressBar1.Value = ((position / bytesToWrite.Length) * 100)
            Application.DoEvents()
            position += intToCopy
        Loop While position < bytesToWrite.Length
    End Using
End Sub

答案 2 :(得分:-1)

'Or, if you are reading a file from a URL, here is a way to read the file into an array of bytes.

Dim WebRequest As Net.HttpWebRequest = Net.WebRequest.Create("http://mypage.abc.com/myfolder/MyFileName.xls")

Using WBinReader As BinaryReader = New BinaryReader(WRequest.GetResponse.GetResponseStream)
    Dim file_buffer() As Byte
    '10000000 is an arbitrary number, but File_Buffer will have no more 
    'elements than they number of bytes in the URL's file.
    file_buffer = WBinReader.ReadBytes(10000000) 
    file_bytes =UBound(buffer)+1
End Using

Brian Jasmer