vb.net 在 Visual Basic 中设置基于目录复制的进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22303811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:01:31  来源:igfitidea点击:

Setting A Progress Bar Based On Directory Copy in Visual Basic

vb.netprogress-barvb.net-2010

提问by xRuhRohx

I am using Visual Studio 2010 and coding in Visual Basic.

我正在使用 Visual Studio 2010 并在 Visual Basic 中编码。

I have to display a progress bar while copying a directory. I have never worked with a progress bar before and not sure where to start. Here is the code I currently have.

复制目录时我必须显示进度条。我以前从未使用过进度条,也不知道从哪里开始。这是我目前拥有的代码。

If My.Computer.FileSystem.DirectoryExists(filePath & "IETMS\" & installFile) Then
    frmWait.Show()

    My.Computer.FileSystem.CopyDirectory(strFileName, filePath & "IETMS", True)

    ListView1.Items.Clear()
    testReload()
    frmWait.Close()
Else
    My.Computer.FileSystem.CreateDirectory(filePath & "IETMS\" & installFile)

    frmWait.Show()

    My.Computer.FileSystem.CopyDirectory(strFileName, filePath & "IETMS", True)

    ListView1.Items.Clear()
    testReload()
    frmWait.Close()
 End If

I am assuming that I need to calculate the size of the source folder and then monitor the destination folder size and set the progress bar max to the source folder size and set the value of the progress bar to the destination size, but I am not sure how to go about doing this.

我假设我需要计算源文件夹的大小,然后监视目标文件夹大小并将进度条最大值设置为源文件夹大小并将进度条的值设置为目标大小,但我不确定如何去做这件事。

回答by Jens

You can count the files in the source directory and then every so often count the files in the destination directory. To count the files in all subdirectories you can use a recursive sub:

您可以计算源目录中的文件,然后每隔一段时间计算目标目录中的文件。要计算所有子目录中的文件,您可以使用递归子:

Private Sub CountFiles(InFolder As String, ByRef Result As Integer)
    Result += IO.Directory.GetFiles(InFolder).Count
    For Each f As String In IO.Directory.GetDirectories(InFolder)
        CountFiles(f, Result)
    Next
End Sub

To use this do

要使用这个做

Dim FileCount as Integer = 0
CountFiles("C:\test", FileCount)
Messagebox.Show(FileCount.ToString)

Set the progressbar to the percentage value like pbProgress.Value = CInt(DestCount/SourceCount * 100).

将进度条设置为百分比值,如pbProgress.Value = CInt(DestCount/SourceCount * 100)

Edit: Following up on your question: You should use for example a backgroundworker, or a task, or a thread, to perform the copy and then update the progressbar in a Timer. For example you can create a sub that does the copying and then start the sub in a new task: Private WithEvents tmrUpdatePBG As Timer Private Sub StartCopy(SourceFolder As String, DestFolder As String) 'copy copy copy CopyComplete() End Sub Private Sub CopyComplete() tmrUpdatePBG.Stop() End Sub

编辑:跟进您的问题:您应该使用例如后台工作者、任务或线程来执行复制,然后更新计时器中的进度条。例如,您可以创建一个执行复制的子程序,然后在新任务中启动子程序: Private WithEvents tmrUpdatePBG As Timer Private Sub StartCopy(SourceFolder As String, DestFolder As String) 'copy copy copy CopyComplete() End Sub Private Sub CopyComplete () tmrUpdatePBG.Stop() End Sub

[...]
'Whereever you start the copy process
Dim ct As New Task(Sub() StartCopy("C:\source", "C:\dest"))
ct.Start()
tmrUpdatePBG = New Timer
tmrUpdatePBG.Interval = 1000
tmrUpdatePBG.Start()

tmrUpdatePGBwould be the timer. In the tick event update the progressbar. It is started when the copying process starts and stops when the process is complete.

tmrUpdatePGB将是计时器。在滴答事件中更新进度条。它在复制过程开始时启动,并在过程完成时停止。

回答by xRuhRohx

I ended up counting the files in the source folder and setting the progress bar max to that number. Then inside the timer I counted the files in the destination folder and set the progress bar value to that number. Then just closed the window I created with the progress bar after the copy was finished.

我最终计算了源文件夹中的文件并将进度条最大值设置为该数字。然后在计时器内部计算目标文件夹中的文件并将进度条值设置为该数字。然后在复制完成后关闭我用进度条创建的窗口。

I also had an issue with the progress bar (not responding) so I put the CopyDirectory inside a BackgroundWorker.

我的进度条也有问题(没有响应),所以我将 CopyDirectory 放在 BackgroundWorker 中。

Private Sub tmrWait_Tick(sender As System.Object, e As System.EventArgs) Handles tmrWait.Tick

        Dim srcFile As String = strFileName & "\" & installFile
        Dim srcDir As New System.IO.DirectoryInfo(srcFile)
        Dim srcFolders, srcFiles As Integer
        srcFolders = srcDir.GetDirectories.GetUpperBound(0) + 1
        srcFiles = srcDir.GetFiles.GetUpperBound(0) + 1
        pbInstall.Maximum = srcFolders.ToString()

        Dim desFile As String = filePath & "IETMS\" & installFile & "\" & installFile
        Dim desDir As New System.IO.DirectoryInfo(desFile)
        Dim desFolders, desFiles As Integer
        desFolders = desDir.GetDirectories.GetUpperBound(0) + 1
        desFiles = desDir.GetFiles.GetUpperBound(0) + 1
        pbInstall.Value = desFolders.ToString()

        pbInstall.Refresh()

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        System.Threading.Thread.Sleep(1000)
        My.Computer.FileSystem.CopyDirectory(strFileName, filePath & "IETMS\" & installFile, True)

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        frmMain.ListView1.Items.Clear()
        frmMain.testReload()
        Me.Close()

    End Sub

    Private Sub frmWait_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        BackgroundWorker1.RunWorkerAsync()

    End Sub