我正在制作一个涉及将大量文件附加到单个文件末尾的应用程序......我正在使用带有缓冲区的文件流类以避免将整个文件加载到内存中,但是,我想显示进度复制每个单独的文件,以及当前文件的名称......这很容易,但是,如果每个文件非常小,在 foreach 循环接缝中这样做会显着降低性能。
这是代码:
Public Function StreamAppendFileToFile(ByVal f1 As String, ByVal f2 As String)
Dim bytesRead As Integer
Dim nn As New FileInfo(f1)
CurrentFsize = nn.Length
Dim buffer(40096) As Byte
Using inFile As New System.IO.FileStream(f1, IO.FileMode.Open, IO.FileAccess.Read)
Using outFile As New System.IO.FileStream(f2, IO.FileMode.Append, IO.FileAccess.Write)
Do
bytesRead = inFile.Read(buffer, 0, 40096)
outFile.Write(buffer, 0, bytesRead)
Application.DoEvents()
Loop While bytesRead > 0
End Using
End Using
End Function
如果我放这样的东西,执行时间会加倍:
Public Function StreamAppendFileToFile(ByVal f1 As String, ByVal f2 As String)
Dim bytesRead As Integer
Dim nn As New FileInfo(f1)
CurrentFsize = nn.Length
Dim buffer(40096) As Byte
Using inFile As New System.IO.FileStream(f1, IO.FileMode.Open, IO.FileAccess.Read)
Using outFile As New System.IO.FileStream(f2, IO.FileMode.Append, IO.FileAccess.Write)
Do
bytesRead = inFile.Read(buffer, 0, 40096)
**Progressbar1.value = Math.Round((bytesRead / CurrentFsize) * 100)**
**Application.Doevents()**
outFile.Write(buffer, 0, bytesRead)
Application.DoEvents()
Loop While bytesRead > 0
End Using
End Using
End Function
在将一个文件流式附加到另一个文件并显示进度方面,是否有更好/更快/更有效的方法来做到这一点?谢谢..