Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively walking through a directory tree and listing file names

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.

Here is the whole project solution in 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

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);
            }
        }
    }
}
like image 262
Steve Way Avatar asked Mar 15 '13 23:03

Steve Way


People also ask

How do I list all files in a directory recursively?

Try any one of the following commands to see recursive directory listing: ls -R : Use the ls command to get recursive directory listing on Linux. find /dir/ -print : Run the find command to see recursive directory listing in Linux. du -a . : Execute the du command to view recursive directory listing on Unix.

How do I list files in a directory and its subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively. That shows you the whole directory tree starting at the current directory (or the directories you name on the command line).


2 Answers

There is a complete example on the Microsoft support site

The issue here is that you want to call DirSearch from the event handler, but it appears you're trying to define the method DirSearch inside the event handler. This is not valid.

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);
    }
}
like image 102
p.s.w.g Avatar answered Oct 21 '22 18:10

p.s.w.g


Use GetDirectories() overload accepting SearchOption:

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

or better EnumerateFiles():

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

Notice it performs lazy filesystem scan.

like image 26
abatishchev Avatar answered Oct 21 '22 19:10

abatishchev