子目录中的文件

时间:2015-07-21 12:59:02

标签: vb.net

有人可以帮我实现将子目录中的文件添加到我的列表框中吗?我从谷歌那里寻找了一些信息,但这些例子中的源代码真的不同于我和我在VB.NET中的新手。我想它可能是System.IO.SearchOption.AllDirectories但我不知道如何在我的代码中实现它。

    Private Sub ButtonFolder_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFolder.Click
    FolderBrowserDialog1.ShowDialog()
    FilePathLabel.Text = System.IO.Path.GetFileName(FolderBrowserDialog1.SelectedPath)
    Dim folder As New IO.DirectoryInfo(System.IO.Path.GetFullPath(FolderBrowserDialog1.SelectedPath))
    Dim arrfile() As IO.FileInfo
    Dim file As IO.FileInfo
    arrfile = folder.GetFiles("*.*")
    dicPaths.Clear()
    For Each file In arrfile
        'ListBox1.Items.Add(file.FullName)
        dicPaths.Add(file.Name, file.FullName)
    Next file
    For Each item As String In dicPaths.Keys
        ListBox1.Items.Add(item)
    Next item
    Label1.Text = "Total Items : " + ListBox1.Items.Count.ToString
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Public Function FindFiles(ByVal Path As String) As Boolean
Dim Directories As New IO.DirectoryInfo(Path)
Dim Directory As IO.DirectoryInfo
Dim File As IO.FileInfo
For Each Directory In Directories.GetDirectories
For Each File In Directory.GetFiles
ListBox1.Items.Add(File.Name)
Next

If Directory.GetDirectories.Length > 0 Then
FindFiles(Directory.FullName)
End If
Next
End Function

用法:FindFiles(FolderBrowserDialog1.SelectedPath)