简体   繁体   English

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

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

I need to make a tool that will pick every .txt file in its folder and subfolders. 我需要制作一个工具来选择其文件夹和子文件夹中的每个.txt文件。 I came to a point that the tool will pick every .txt file in its folder, but I don't know how to instantly also pick from all the subfolders. 我的观点是,该工具将选择其文件夹中的每个.txt文件,但我不知道如何立即从所有子文件夹中进行选择。

Here is a code sample to let you know what I did, hope you could help me. 这是一个代码示例,让您知道我做了什么,希望您能对我有所帮助。

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

You should use dinfo.GetFiles("*.txt", SearchOption.AllDirectories) as said in the link: 您应该按照链接中所述使用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 https://msdn.microsoft.com/pl-pl/library/ms143448(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM