Vb.net 读/写二进制文件
我有这样一个代码,它工作得很好:Vb.net 读/写二进制文件,vb.net,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 B
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
我有这个代码来保存文件,它也可以很好地工作:
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
My.Computer.Filesystem.ReadAllBytes(“文件名”)将整个文件读入字节数组
My.Computer.Filesystem.writealBytes(“filename”,bytes,false)将其写回 我从未尝试在FileStream上使用异步选项,但似乎没有任何事件处理程序 我会把它分解成一个循环,你有总的字节数要写,这样你就可以一次写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
Public子SaveBinaryFile(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,缓冲区长度)
ProgressBar1.Value=((position/bytesToWrite.Length)*100)
Application.DoEvents()
位置+=内部复制
位置
布莱恩·贾斯默太棒了,谢谢马特,效果很好。我刚刚意识到,我原来的LoadBinaryFile函数实际上是一次加载所有字节,而不是使用缓冲区(这样我就可以报告进程的百分比)。你能做一个与你的SaveBinaryFile相似的功能,只是加载文件吗?如果你导入Soal.IOI,Fiel.RealAlelyByter和Fiel.WrreErrAlxBythor也会起作用。我不确定这是否为OP的问题提供了答案,考虑在评论中留下这样的建议。