我編寫了一個Windows窗體腳本,該腳本搜索系統中的所有非隱藏和非只讀文件夾。但腳本本身最初運行時會運行5分鐘。隨後的開場時間要少得多。我想知道它是否存在邏輯錯誤,以至於它的運行速度如此之慢。遞歸搜索系統中的所有文件夾和子文件夾
Private Function FindSubFolders(ByVal dir As DirectoryInfo, ByVal node As TreeNode) As TreeNode
Dim subnode As New TreeNode
For Each folder As DirectoryInfo In dir.GetDirectories()
If (folder.Attributes And FileAttributes.Hidden) <> FileAttributes.Hidden Then
subnode = node.Nodes.Add(folder.FullName, folder.Name)
subnode = FindSubFolders(folder, subnode)
End If
Next
Return subnode
End Function
Private Sub SetFolders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Is it possible to load this on 1st (initial) form load???
Try
Dim node As TreeNode
If TreeView1.Nodes.Count < 1 Then
For Each drive As String In Directory.GetLogicalDrives
Directory.GetLogicalDrives()
Dim folders As DirectoryInfo = New DirectoryInfo(drive)
If (folders.Attributes And FileAttributes.ReadOnly) <> FileAttributes.ReadOnly Then
node = TreeView1.Nodes.Add(drive, drive)
Try
node = FindSubFolders(folders, node)
Catch ex As Exception
Console.WriteLine(ex.Message)
Continue For
End Try
End If
Next
End If
If Not IsNothing(My.Settings.Folders) Then
If ListBox1.Items.Count < 1 Then
For Each col As String In My.Settings.Folders
ListBox1.Items.Add(col)
Next
End If
Else
My.Settings.Folders = New StringCollection
End If
Catch ex As Exception
Logs.Add("04", ex.Message)
End Try
Logs.Add("01", "Loaded.")
End Sub
感謝您的幫助! :)
檢查這個線程:http://stackoverflow.com/questions/2428136/get-all-folder -directories-list-in-vb-net – Stefan 2011-03-24 08:35:58
做某種*基準測試*可能有助於確定何時完全丟失時間。 System.Diagnostics.Stopwatch類可用於此目的。 – Heinzi 2011-03-24 08:48:18