如何创建进度条,该进度条与文件传输同时更新

时间:2019-04-05 20:44:08

标签: vb.net progress-bar file-transfer

正如标题所示,我试图创建一个进度条,该进度条随文件传输而更新。我目前正在使用Visual Studio2019。我浏览了数十篇文章和视频,声称它们都是这样做的。经过数天的测试,我已经关闭了,但是进度栏仍然只会在文件传输完成之后更新。我正在使用多线程技术来完成这项工作。如果有人能为我提供帮助,我将不胜感激。到目前为止,这是我的代码。它实际上并没有帮助,但是至少您可以看到我正在尝试实现的目标。我还省略了一些很大的注释掉的测试脚本。

我需要做的总结是:创建一个脚本,该脚本将复制指定的目录和所有子目录。在执行此操作时,我希望进度条随文件传输一起移动。

Imports System.ComponentModel
Imports System.Threading
Imports System
Imports System.IO

Public Class Form1
    Private Sub BtnStartTransfer_Click(sender As Object, e As EventArgs) Handles btnStartTransfer.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Delegate Sub DelegateProgressBarMax(ByVal check As Integer)

    Private Sub ProgressBarUpdate(ByVal check As Integer)
        If pBar1.InvokeRequired = True Then
            Invoke(Sub() pBar1.Value = check)
        Else
            pBar1.Value = check
        End If
    End Sub

    Private Delegate Sub DelegateUpdateOutput(ByVal check2 As String)
    Private Sub OutputUpdate(ByVal check2 As String)
        If txtOutput.InvokeRequired = True Then
            Invoke(Sub() txtOutput.Text = txtOutput.Text & check2 & Environment.NewLine)
        Else
            txtOutput.Text = txtOutput.Text & check2
        End If
    End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim getCopyFrom As String = txtCopyFrom.Text
        Dim getCopyTo As String = txtCopyTo.Text

        Dim splitUser() As String = getCopyFrom.Split("\")
        Dim finalValue As String = splitUser.Length - 1
        Dim stringValue As String = CStr(splitUser(finalValue))
        Dim getUser As String

        'If MsgBox("Is this the correct user?: " & stringValue, vbYesNo + vbQuestion) = vbYes Then
        '    getUser = stringValue
        'Else
        '    getUser = InputBox("Enter in the correct Username")
        'End If

        Dim checkCopyFrom As New IO.DirectoryInfo(getCopyFrom)
        Dim checkCopyTo As New IO.DirectoryInfo(getCopyTo)

        If checkCopyFrom.Exists Then

        Else
            MsgBox("The location you are trying to copy from does not exist.")
            Exit Sub
        End If

        If checkCopyTo.Exists Then

        Else
            MsgBox("The location you are trying to copy to does not exist.")
            Exit Sub
        End If

        'Copying the Desktop folder
        Dim dirDesktop = getCopyFrom & "\Desktop"
        Dim getDir = IO.Directory.GetFiles(dirDesktop, "*", IO.SearchOption.AllDirectories)
        Dim fileTotal As Integer = getDir.Length
        Dim filesTransferred As Integer = 0

        Dim di As New DirectoryInfo(dirDesktop)
        Dim fiArr As FileInfo() = di.GetFiles("*", SearchOption.AllDirectories)
        Dim diArr As DirectoryInfo() = di.GetDirectories("*", IO.SearchOption.AllDirectories)
        Dim fri As FileInfo
        Dim fol As DirectoryInfo

        For Each fri In fiArr
            filesTransferred += 1
            BackgroundWorker1.ReportProgress(CInt(filesTransferred * 100 \ fiArr.Length), True)
            OutputUpdate(fri.Name)
            'File.Copy(dirDesktop & "\" & fri.Name, getCopyTo & "\" & fri.Name, True)
            'My.Computer.FileSystem.CopyDirectory(getCopyFrom & "\Desktop", getCopyTo & "\Users\" & getUser & "\Desktop", False)
        Next fri
End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        pBar1.Value = e.ProgressPercentage
    End Sub

0 个答案:

没有答案