C# 递归遍历目录树并列出文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15443815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:51:14  来源:igfitidea点击:

Recursively walking through a directory tree and listing file names

c#recursiondirectory

提问by Steve Way

I'm trying to walk through a whole directory tree and print out all the file names on a listbox control. I wrote some code but there are errors. Not sure what I'm doing wrong. By the way, this is in C# using WPF in Visual Studio.

我试图遍历整个目录树并打印出列表框控件上的所有文件名。我写了一些代码,但有错误。不知道我做错了什么。顺便说一下,这是在 C# 中使用 Visual Studio 中的 WPF。

Here is the whole project solution in Visual Studio: http://tinyurl.com/a2r5jv9

这是 Visual Studio 中的整个项目解决方案:http: //tinyurl.com/a2r5jv9

Here is the code from MainWindow.xaml.cs if you don't want to download the project solution: http://pastebin.com/cWRTeq3N

如果您不想下载项目解决方案,这里是 MainWindow.xaml.cs 中的代码:http: //pastebin.com/cWRTeq3N

I'll paste the code here as well.

我也会在这里粘贴代码。

public partial class MainWindow : Window
{
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        string sourcePath = @"C:\temp\";            

        static void DirSearch(string sourcePath)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sourcePath))
                {
                    foreach (string f in Directory.GetFiles(d))
                    {
                        listBox1.Items.Add(f);
                    }
                    DirSearch(d);
                }
            }                      
            catch (Exception ex)
            {
                listBox1.Items.Add(ex.Message);
            }
        }
    }
}

采纳答案by p.s.w.g

There is a complete exampleon the Microsoft support site

Microsoft 支持站点上有一个完整的示例

The issue here is that you want to callDirSearchfrom the event handler, but it appears you're trying to definethe method DirSearchinside the event handler. This is not valid.

这里的问题是您想从事件处理程序调用DirSearch,但似乎您正在尝试在事件处理程序中定义方法DirSearch。这是无效的。

You need to change your code as follows:

您需要按如下方式更改代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    string sourcePath = @"C:\temp\";
    this.DirSearch(sourcePath);
}

private void DirSearch(string sDir) 
{
    try 
    {
        foreach (string f in Directory.GetFiles(sDir, txtFile.Text)) 
        {
            lstFilesFound.Items.Add(f);
        }

        foreach (string d in Directory.GetDirectories(sDir)) 
        {
            this.DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        listBox1.Items.Add(ex.Message);
    }
}

回答by abatishchev

Use GetDirectories() overload accepting SearchOption:

使用接受 SearchOption 的 GetDirectories() 重载

string[] dirs = Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
foreach(dir)
{
    ...
}

or better EnumerateFiles():

或更好的EnumerateFiles()

IEnumerable<string> files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
foreach(files)
{
    ...
}

Notice it performs lazyfilesystem scan.

请注意,它执行延迟文件系统扫描。