一尘不染

How to recursively list all the files in a directory in C#?

c#

How to recursively list all the files in a directory and child directories in
C#?


阅读 227

收藏
2020-05-19

共1个答案

一尘不染

This article covers all you need.
Except as opposed to searching the files and comparing names, just print out
the names.

It can be modified like so:

static void DirSearch(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                Console.WriteLine(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}
2020-05-19