搜索并将目录/子目录中的所有文件列入ListBox

时间:2014-09-08 12:05:05

标签: vb.net file search listbox getfiles

好的解决了我对@sircodesalot的旧问题 Public Sub AppendFilesFromDirectory(ByVal path As DirectoryInfo, ByVal files As List(Of FileInfo)) For Each file As FileInfo In CurrentFolder.GetFiles() files.Add(file) Next For Each subfolder As DirectoryInfo In CurrentFolder.GetDirectories() AppendFilesFromDirectory(subfolder, files) Next End Sub

Dim files As New List(Of FileInfo) AppendFilesFromDirectory(New DirectoryInfo(FolderBrowserDialog1.SelectedPath), files) For Each file As FileInfo In files ListBox1.Items.Add(file.FullName) Next


它适用于包含子文件夹的文件夹,但如果文件夹只有文件,则它会成为一个循环,将文件连续添加到myList。

有什么建议可以避免吗? @sircodesalot亲切地试图解释我,但是我没有能够做我看过的那个...

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

首先了解一下像factorial这样的简单递归函数。例如:

int factorial(int number) {
    if (number < 1)
        return 1;
    else
        return factorial(number - 1) * number;
}

基本上,如果我们想要计算factorial(5),那么它只是:

 factorial(5) * factorial(4) * factorial (3) * factorial(2) * factorial(1)

或更抽象地说:

 factorial(n) * factorial(n - 1) * factorial (n - 2) * ... * factorial (1)

因此,我们使用递减值使函数调用本身来计算结果。

同样适用于上述问题。如果我们想获得所有子目录,那么我们所要做的就是:

(1) List all the files in the current folder
(2) For the files that are directories, repeat step one.

换句话说:

List<Folder> readAllFiles() {
    List<Folder> folders = new List<Folder>();

    readAllFilesRecursively("C:/", folders);
}

List<Folder> readAllFilesRecursively(String directory, List<Folder> folders) {
     Folder currentDirectory = castTheStringToADirectorySomehow(directory);

     // Add the current folder to the list.
     folders.add(currentDirectory);


     // ** HERE IS WHAT YOU'RE MISSING ** //
     // Re-call the same function on the sub folder, passing in our list
     // so that the subfolder items can be added to the list.
     foreach (Folder subfolder in currentDirectory.subFolders()) {
          readAllFilesRecursively(subFolder.pathString(), folders);
     }

     // Return the folder list.
     return folders;
}

编辑:

所以这对我有用,但你必须明确地将其改为VB。此外,我在Mac上,所以你会注意到路径格式有点不同:

class Program {
    public static void Main() {

        List<FileInfo> files = new List<FileInfo> ();
        AppendFilesFromDirectory (new DirectoryInfo("/Users/sircodesalot/Desktop/Dev"), files);

        foreach (FileInfo file in files) {
            Console.WriteLine (file.FullName);
        }
    }

    public static void AppendFilesFromDirectory(DirectoryInfo currentFolder, List<FileInfo> files) {

        foreach (FileInfo file in currentFolder.GetFiles()) {
            files.Add(file);
        }

        // This if statement is unneccesary, but I'll add it for clarity 
        // to explain the concept of a base case. We don't really need it though
        // since the 'foreach' statement won't execute if there aren't any items.

        if (currentFolder.GetDirectories().Count() > 0) {

            // Process the subfolders
            foreach (DirectoryInfo subfolder in currentFolder.GetDirectories()) {
                AppendFilesFromDirectory (subfolder, files);
            }
        }
    }
}