且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从目录VB.NET中的文件夹和子文件夹中获取所有列表框文件的替代方法

更新时间:2023-09-25 20:44:40

哦,你走了:

  Imports  System.IO 

私有 Sub scanFolders( ByVal FolderLocation As String
尝试
对于 每个 d Directory.GetDirectories(FolderLocation)
ListBox1.Items.Add(d)
For 每个 f Directory.GetFiles(d, *。*
ListBox1.Items.Add(f)
Next
scanFol ders(d)
下一步
Catch ex 作为例外
' 日志错误
结束 尝试
结束 子跨度>


the current way Ive got inst working well it sometimes doesn't work is there another way to the one below or a way to improve :

Sub scanSubfolders(ByVal FolderLocation As String, ByVal lstbox As ListBox)
        For Each s In My.Computer.FileSystem.GetFiles(FolderLocation)
            Try
                lstbox.Items.Add(s)

            Catch ex As Exception

            End Try
        Next
        For Each s In My.Computer.FileSystem.GetDirectories(FolderLocation)
            Try
                scanSubfolders(s, ListBox1)
            Catch ex As Exception

            End Try
        Next
    End Sub


cheers


oh and form load

scanSubfolders(My.Computer.FileSystem.SpecialDirectories.Temp, Me.ListBox1)



What I have tried:

i wrote above what ive tried ect so hope someone could help.

Oh, here you go:
Imports System.IO

 Private Sub scanFolders(ByVal FolderLocation As String)
      Try
         For Each d In Directory.GetDirectories(FolderLocation)
            ListBox1.Items.Add(d)
            For Each f In Directory.GetFiles(d, "*.*")
               ListBox1.Items.Add(f)
            Next
            scanFolders(d)
         Next
      Catch ex As Exception
         ' log error
      End Try
   End Sub