VB.NET - Select 所有 .txt 文件,包括子目录

VB.NET - Select all .txt files, including sub directory

我需要制作一个工具来挑选其文件夹和子文件夹中的每个 .txt 文件。我发现该工具会选择其文件夹中的每个 .txt 文件,但我不知道如何立即从所有子文件夹中选择。

这是一个代码示例,让你知道我做了什么,希望你能帮助我。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim fbd As FolderBrowserDialog = New FolderBrowserDialog() With {
            .Description = "Select a path",
            .SelectedPath = "C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\WindowsApp2\WindowsApp2\bin\Debug"
        }

        If fbd.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = fbd.SelectedPath
        End If

        Dim dinfo As New DirectoryInfo(TextBox1.Text)

        Dim files As FileInfo() = dinfo.GetFiles("*.txt")

        ListBox1.Items.Clear()
        For Each file As FileInfo In files
            ListBox1.Items.Add(file.Name)
        Next

    End Sub

您应该使用 dinfo.GetFiles("*.txt", SearchOption.AllDirectories),如 link 中所述:

https://msdn.microsoft.com/pl-pl/library/ms143448(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2