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

时间:2017-05-19 07:13:57

标签: .net vb.net file folderbrowserdialog

我需要创建一个工具来选择其文件夹和子文件夹中的每个.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

1 个答案:

答案 0 :(得分:1)

您应该使用链接中所述的dinfo.GetFiles("*.txt", SearchOption.AllDirectories)

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