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

前端 未结 22 2502
长发绾君心
长发绾君心 2020-11-22 00:07

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

22条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:37

    In Framework 2.0 you can use (It list files of root folder, it's best the most popular answer):

    static void DirSearch(string dir)
    {
        try
        {
            foreach (string f in Directory.GetFiles(dir))
                Console.WriteLine(f);
            foreach (string d in Directory.GetDirectories(dir))
            {
                Console.WriteLine(d);
                DirSearch(d);
            }
    
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    

提交回复
热议问题