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

前端 未结 22 2396
长发绾君心
长发绾君心 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:58

    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);
        }
    }
    

    Added by barlop

    GONeale mentions that the above doesn't list the files in the current directory and suggests putting the file listing part outside the part that gets directories. The following would do that. It also includes a Writeline line that you can uncomment, that helps to trace where you are in the recursion that may help to show the calls to help show how the recursion works.

                DirSearch_ex3("c:\\aaa");
                static void DirSearch_ex3(string sDir)
                {
                    //Console.WriteLine("DirSearch..(" + sDir + ")");
                    try
                    {
                        Console.WriteLine(sDir);
    
                        foreach (string f in Directory.GetFiles(sDir))
                        {
                            Console.WriteLine(f);
                        }
    
                        foreach (string d in Directory.GetDirectories(sDir))
                        {
                            DirSearch_ex3(d);
                        }
                    }
                    catch (System.Exception excpt)
                    {
                        Console.WriteLine(excpt.Message);
                    }
                }
    
    0 讨论(0)
  • 2020-11-22 00:58
    var d = new DirectoryInfo(@"C:\logs");
    var list = d.GetFiles("*.txt").Select(m => m.Name).ToList();
    
    0 讨论(0)
  • 2020-11-22 01:00

    Here's my angle on it, based on Hernaldo's, if you need to find files with names of a certain pattern, such as XML files that somewhere in their name contain a particular string:

    // call this like so: GetXMLFiles("Platypus", "C:\\");
    public static List<string> GetXMLFiles(string fileType, string dir)
    {
        string dirName = dir; 
        var fileNames = new List<String>();
        try
        {
            foreach (string f in Directory.GetFiles(dirName))
            {
                if ((f.Contains(fileType)) && (f.Contains(".XML")))
                {
                    fileNames.Add(f);
                }
            }
            foreach (string d in Directory.GetDirectories(dirName))
            {
                GetXMLFiles(fileType, d);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return fileNames;
    }
    
    0 讨论(0)
  • 2020-11-22 01:00

    A very simple solution, returns a list of files.

        public static List<string> AllFilesInFolder(string folder)
        {
            var result = new List<string>();
    
            foreach (string f in Directory.GetFiles(folder))
            {
                result.Add(f);
            }
    
            foreach (string d in Directory.GetDirectories(folder))
            {
                result.AddRange(AllFilesInFolder(d));
            }
    
            return result;
        }
    
    0 讨论(0)
提交回复
热议问题