【问题标题】:Get percentage of files copied from a list获取从列表中复制的文件的百分比
【发布时间】:2019-09-17 18:02:35
【问题描述】:

我有一个将列表中的文件复制到新目录的应用程序。我正在尝试使进度条正常工作,但我的公式导致 System.OverflowException:算术运算导致溢出。我查看了使用文件复制的其他示例,但它们的中心是使用源文件夹除以目标文件夹。挖掘有点不同,因为我使用的是列表中的文件。

感谢您的帮助。

代码

        Dim progress = fileCount * 100 / fileList.Count()
        BackgroundWorker1.ReportProgress(progress)

获取文件计数的代码

Dim FILE_NAME As String

    FILE_NAME = txtFileName.Text
    Dim fileNames = System.IO.File.ReadAllLines(FILE_NAME)

    fCount = 0
    For i = 0 To fileNames.Count() - 1
        Dim fileName = fileNames(i)
        sFileToFind = location & "\" & fileName & "*.*"
        Dim paths = IO.Directory.GetFiles(location, fileName, IO.SearchOption.AllDirectories)
        If Not paths.Any() Then
            System.IO.File.AppendAllText(orphanedFiles, fileName & vbNewLine)
        Else
            For Each pathAndFileName As String In paths
                If System.IO.File.Exists(pathAndFileName) = True Then
                    sRegLast = pathAndFileName.Substring(pathAndFileName.LastIndexOf("\") + 1)
                    Dim toFileLoc = System.IO.Path.Combine(createXMLFldr, sRegLast)
                    Dim moveToFolder = System.IO.Path.Combine(MoveLocation, "XML files", sRegLast)

                    'if toFileLoc = XML file exists move it into the XML files folder
                    If System.IO.File.Exists(toFileLoc) = False Then
                        System.IO.File.Copy(pathAndFileName, moveToFolder)
                        System.IO.File.AppendAllText(ListofFiles, sRegLast & vbNewLine)
                        fCount = fCount + 1

                        XMLFilename = (sRegLast) + vbCrLf
                    End If
                End If
            Next
        End If

        Dim srcCount = paths.Count()
        If (paths.Count() = 0) Then
            srcCount = 1
        End If


        Dim progress = CType(fCount * 100 / srcCount, Integer)
        BackgroundWorker1.ReportProgress(progress)


    Next

【问题讨论】:

  • 你需要检查fileList.Count = 0是否被0除。
  • @dashier 我添加了一个 if 语句,如果 fileList.Count 为零,则将其更改为一。但我仍然得到 OverflowException
  • 您需要显示最少的代码才能重现问题。 不是您的整个程序,但足以让我们看到fileList是如何创建/填充的。如果您使用调试器并查看值会发生什么?还有,BackgroundWorker1.ReportProgress()要求的类型是什么?
  • 文件数如何计算?
  • 我有一个包含文件列表的文本文件。我将所有行读入 fileList

标签: vb.net progress-bar


【解决方案1】:

这个给BackGroundWorker,希望能解决下次调用更新进度条的麻烦:

'This button1_click event to begin or stop async of backGrounedWorker
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    BackgroundWorker1.WorkerReportsProgress = True
    BackgroundWorker1.WorkerSupportsCancellation = True
    BackgroundWorker1.CancelAsync()
    Try
        BackgroundWorker1.RunWorkerAsync()
    Catch ex As Exception
    End Try
End Sub

'Here your backGroundProcess
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim FILE_NAME As String

    FILE_NAME = txtFileName.Text 'can be changed
    Dim fileNames = System.IO.File.ReadAllLines(FILE_NAME)

    Dim Location As String = "C:\MyData\Folder1" 'can be changed
    Dim moveToFolder As String = "C:\MyData\Folder2" 'can be changed
    Dim fCount As Integer = 0
    For Each fileName In fileNames

        Dim paths = IO.Directory.GetFiles(Location, fileName,
                                          IO.SearchOption.AllDirectories)

        If paths.Count > 0 Then
            Try
                System.IO.File.Copy(paths(0), moveToFolder & "\" & fileName, True)
            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try
        End If
        For a = 1 To 100
            Debug.Print(a.ToString)
        Next

        'If any request pending process will be stop (when Button1_click happen while process running (BackGroundWorker in Action))
        If BackgroundWorker1.CancellationPending = True Then Exit For

        fCount += 1
        Dim Progress As Integer = Convert.ToInt32(fCount * 100 / fileNames.Count)
        BackgroundWorker1.ReportProgress(progress)
    Next
End Sub

'This will show you the progress in your progress bar
Private Sub bw1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
End Sub

'Your Process Have Been Complete Here
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MessageBox.Show("Finish")
End Sub

【讨论】:

    【解决方案2】:

    我最后的修改:

        ListBox1.Items.Clear()
        Dim FILE_NAME As String
    
        FILE_NAME = "c:\YourFolder\YourFile.txt" 'can be changed
        Dim fileNames = System.IO.File.ReadAllLines(FILE_NAME)
    
        For Each MyFile In fileNames
            ListBox1.Items.Add(MyFile)
        Next
    
        Dim Location As String = "C:\YourFolder1" 'can be changed
        Dim moveToFolder As String = "C:\YourFolder2" 'can be changed
        Dim fCount As Integer = 0
        For Each fileName In fileNames
    
            Dim paths = IO.Directory.GetFiles(Location, fileName,
                                              IO.SearchOption.AllDirectories)
    
            If paths.Count > 0 Then
                Try
                    System.IO.File.Copy(paths(0), fileName)
                Catch ex As Exception
                End Try
            End If
            fCount += 1
            Dim progress As Integer = Convert.ToInt32(fCount * 100 / fileNames.Count)
        Next
    

    【讨论】:

    • @answered 这很好用,直到我第二次使用 progress = Convert.ToInt32(iCount * 100 / ListDedupedGraphics.Count) 调用进度条。只到了 49%,然后说复制的文件是完整的
    • 为了100%的结果,无论您搜索的文件是否可以找到,您必须将else语句下标记的fCount += 1更改为未标记。而关于进度条,我觉得可以刷新,比如myprogressbar.refresh。为了更好,您可以在退出 for 循环之前放置进度计算线和进度条操作。
    • 我只是用我添加的一些 cmets 修改了你的编码
    • 另外,你可以数一下找不到多少文件。
    • 如何解决进度条只有 49% 的问题?
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多