Vb.net 按进度复制文件

Vb.net 按进度复制文件,vb.net,Vb.net,我想要最有效的方式来复制一个文件的进度条更新的复制百分比 这是我使用的标准副本代码: System.IO.File.Copy(source,target) Public Sub SaveBinaryFile(strFilename As String, bytesToWrite() As Byte) Dim position As Integer = 0 Dim BufferSize As Integer = 4096 'frmMain.tsProgressBa

我想要最有效的方式来复制一个文件的进度条更新的复制百分比

这是我使用的标准副本代码:

System.IO.File.Copy(source,target)
    Public Sub SaveBinaryFile(strFilename As String, bytesToWrite() As Byte)
    Dim position As Integer = 0
    Dim BufferSize As Integer = 4096
    'frmMain.tsProgressBar.Value = 0

    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
        Do
            Dim intToCopy As Integer = Math.Min(BufferSize, bytesToWrite.Length - position)
            Dim buffer(intToCopy - 1) As Byte
            Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
            fsNew.Write(buffer, 0, buffer.Length)
            'frmMain.tsProgressBar.Value = ((position / bytesToWrite.Length) * 100)
            'frmMain.tsProgressBar.Refresh()
            Application.DoEvents()
            position += intToCopy
        Loop While position < bytesToWrite.Length
    End Using
End Sub

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
这是非常快速和有效的。但是,我无法报告复制百分比。 我已经尝试了许多函数,通过打开文件流,然后在读/写数据时循环来读取和保存文件。这使我能够报告进度

以下是我正在使用的函数:

System.IO.File.Copy(source,target)
    Public Sub SaveBinaryFile(strFilename As String, bytesToWrite() As Byte)
    Dim position As Integer = 0
    Dim BufferSize As Integer = 4096
    'frmMain.tsProgressBar.Value = 0

    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
        Do
            Dim intToCopy As Integer = Math.Min(BufferSize, bytesToWrite.Length - position)
            Dim buffer(intToCopy - 1) As Byte
            Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
            fsNew.Write(buffer, 0, buffer.Length)
            'frmMain.tsProgressBar.Value = ((position / bytesToWrite.Length) * 100)
            'frmMain.tsProgressBar.Refresh()
            Application.DoEvents()
            position += intToCopy
        Loop While position < bytesToWrite.Length
    End Using
End Sub

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
Public子SaveBinaryFile(strFilename作为字符串,bytesToWrite()作为字节)
Dim位置为整数=0
Dim BufferSize为整数=4096
'frmMain.tsProgressBar.Value=0
使用fsNew作为FileStream=newfilestream(strFilename,FileMode.Create,FileAccess.Write)
做
Dim intToCopy As Integer=Math.Min(BufferSize,bytesToWrite.Length-position)
Dim缓冲区(intToCopy-1)作为字节
Copy(bytesToWrite、position、buffer、0、intToCopy)
fsNew.Write(缓冲区,0,缓冲区长度)
'frmMain.tsProgressBar.Value=((position/bytesToWrite.Length)*100)
'frmMain.tsProgressBar.Refresh()
Application.DoEvents()
位置+=内部复制
位置fsOpen.Length,则
读取(字节、位置、fsOpen.Length-位置)
退出Do
其他的
读取(字节、位置、缓冲区大小)
如果结束
'frmMain.tsProgressBar.Value=((位置/fsOpen.Length)*100)
'frmMain.tsProgressBar.Refresh()
Application.DoEvents()
位置+=缓冲区大小
环
终端使用
返回字节
端函数
问题是,这比使用直接复制代码慢得多

复制显示复制进度的文件的最佳/高效方法是什么


谢谢

有一个
System.IO.File的变体
提供用户反馈;它被称为
Microsoft.VisualBasic.FileIO.FileSystem
。另请参见。

您可以使用:

Microsoft.VisualBasic.FileIO.FileSystem.CopyFile("C:\[Copy From]", "C:\[Copy To]", _
                        FileIO.UIOption.AllDialogs)
' this requires the Microsoft.VisualBasic.dll, which is referenced from Visual Basic .NET
' projects by default, but needs to be added to C# projects manually
或:

嗯,试试这个函数

 Dim D As Integer
Function CopyFileWithProgress(ByVal Source As String, ByVal Destination As String) As Integer
    Try

        Dim SourceF As New IO.FileStream(TextBox1.Text, IO.FileMode.Open)
        Dim DestinationF As New IO.FileStream(TextBox2.Text & "\" & TextBox3.Text & ".ps4game", IO.FileMode.Create)
        Dim len As Long = SourceF.Length - 1
        Dim buffer(1024) As Byte
        Dim byteCFead As Integer
        While SourceF.Position < len
            byteCFead = (SourceF.Read(buffer, 0, 1024))
            DestinationF.Write(buffer, 0, byteCFead)
            D = CInt(SourceF.Position / len * 100)
            Application.DoEvents()
        End While
        DestinationF.Flush()
        DestinationF.Close()
        SourceF.Close()
        Return D
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Function
Dim D作为整数
函数CopyFileWithProgress(ByVal源为字符串,ByVal目标为字符串)为整数
尝试
将SourceF设置为新IO.FileStream(TextBox1.Text,IO.FileMode.Open)
作为新IO.FileStream(TextBox2.Text&“\”&TextBox3.Text&“.ps4game”,IO.FileMode.Create)的Dim目标
Dim len As Long=源文件长度-1
Dim缓冲区(1024)作为字节
作为整数的Dim字节头
而SourceF.位置

它将工作

可能重复我尝试过的:My.Computer.FileSystem.CopyFile(sourceFileName、destinationFilename、FileIO.UIOption.AllDialogs),但运气不好。我还增加了“导入微软.VisualBasic .FILIO”,你应该考虑通过代码注释和详细说明你的代码为什么和如何来改进你的解决方案。没有Soop.IO.FIL.EXION,这需要一个UIOPTION参数。@丹尼尔对不起,Bro,我已经独自工作了8年。确切地说,我在windows 8.1上已经使用过好几次了。@Fa3o-在VisualBasic空间中工作的是Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(sourceFileName、destinationFileName、UIOption.AllDialogs)。这可能就是你所记得的。。。我认为马克斯是对的。似乎没有接受UIOption参数的System.IO.File.Copy。所以,你们都说得有点对:-)