【问题标题】:Search and List all files from directory/subdirectories into ListBox搜索并列出目录/子目录中的所有文件到 ListBox
【发布时间】:2014-09-08 12:05:04
【问题描述】:

好的,感谢@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 好心地试图向我解释,但无论我尝试什么,我都无法做到..

非常感谢您的帮助!

【问题讨论】:

  • 我认为你没有意识到这个列表会有多大。
  • 这是一个 C:\ 的例子,当然我有一个变量和我的列表路径。
  • 注意My.Computer.FileSystem.GetDirectories如果没有子文件夹应该为空,所以递归应该停止。
  • 如何在其中插入if并检查它?试了好几次都不行。。
  • 这看起来非常接近正确。有什么不好的地方?

标签: vb.net file search listbox getfiles


【解决方案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);
            }
        }
    }
}

【讨论】:

  • 我忘了说我是在vb.net做的,你能写在vb.net吗?非常感谢!第一篇文章也已编辑。
  • 是的,我注意到了。不幸的是,我并不真正了解 vb.net,但解决方案基本相同。基本上你只需要编写一个函数来捕获当前目录中的所有文件夹,然后为该目录中的每个子文件夹再次调用该函数(从该函数中)。
  • 好的。我想我明白了。但它会带来另一个问题..如果目录只有文件而没有目录..它会在将找到的每个文件添加到列表时变成一个循环..
  • 这就是禅的部分。 if 其中一个文件是目录,您只需再次调用该函数。就像上面的阶乘函数一样,当我们到达 1 时我们停止,因为此时没有什么可做的了。如果没有更多文件夹,那么我们就完成了,我们让函数退出(返回到父目录)。
  • 换句话说,将函数视为列出目录中的所有项目。当您遇到子目录时,您再次调用相同的函数,但这次是在子目录上。如果子目录有一个子目录,那么你也这样做。完成后,我们让函数退出,它会将我们回滚到父目录,依此类推,直到我们用完所有目录中的所有文件。递归编程一开始真的很奇怪,但它确实是解决您的问题的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2012-09-02
  • 2021-01-25
  • 2011-03-01
  • 2022-01-02
相关资源
最近更新 更多