我使用此代码列出目录中的文件:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
rslts = New List(Of String)
foo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
ListBox1.Items.Clear()
ListBox1.Items.AddRange(rslts.ToArray)
End Sub
Dim rslts As List(Of String)
Private Sub foo(ByVal aDir As String)
Try
Dim di As New IO.DirectoryInfo(aDir)
Dim aryFiles() As IO.FileInfo = di.GetFiles("*.*")
Dim aryDirs() As IO.DirectoryInfo = di.GetDirectories()
For Each fi As IO.FileInfo In aryFiles
rslts.Add(fi.FullName)
Next
For Each d As IO.DirectoryInfo In aryDirs
foo(d.FullName)
Next
Catch ex As Exception
End Try
End Sub
我不想使用Math.Round()
,所以我可以在加载文件时为进度条添加增量。我知道我需要使用计时器,但我不熟悉Math.Round()
基本上,在将文件加载到列表框中时,我真的没有真正的进度条。
答案 0 :(得分:1)
您无需执行增量计算,ProgressBar
可以处理所有事情。如何在代码中使用它(ProgressBar1
):
Private Sub foo(ByVal aDir As String)
Try
Dim di As New IO.DirectoryInfo(aDir)
Dim aryFiles() As IO.FileInfo = di.GetFiles("*.*")
Dim aryDirs() As IO.DirectoryInfo = di.GetDirectories()
With ProgressBar1
.Minimum = 0
.Maximum = aryFiles.Count + aryDirs.Count
.Value = 0
.Step = 1
End With
For Each fi As IO.FileInfo In aryFiles
rslts.Add(fi.FullName)
ProgressBar1.PerformStep()
Next
For Each d As IO.DirectoryInfo In aryDirs
foo(d.FullName)
ProgressBar1.PerformStep()
Next
Catch ex As Exception
End Try
End Sub