Method to get all files within folder and subfolders that will return a list

前端 未结 8 1139
别跟我提以往
别跟我提以往 2020-12-04 17:30

I have a method that will iterate through a folder and all of its subfolders and get a list of the file paths. However, I could only figure out how to create it and add the

相关标签:
8条回答
  • 2020-12-04 18:22
    private List<String> DirSearch(string sDir)
    {
        List<String> files = new List<String>();
        try
        {
            foreach (string f in Directory.GetFiles(sDir))
            {
                files.Add(f);
            }
            foreach (string d in Directory.GetDirectories(sDir))
            {
                files.AddRange(DirSearch(d));
            }
        }
        catch (System.Exception excpt)
        {
            MessageBox.Show(excpt.Message);
        }
    
        return files;
    }
    

    and if you don't want to load the entire list in memory and avoid blocking you may take a look at the following answer.

    0 讨论(0)
  • 2020-12-04 18:26

    Simply use this:

    public static List<String> GetAllFiles(String directory)
    {
        return Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories).ToList();
    }
    

    And if you want every file, even extensionless ones:

    public static List<String> GetAllFiles(String directory)
    {
        return Directory.GetFiles(directory, "*", SearchOption.AllDirectories).ToList();
    }
    
    0 讨论(0)
提交回复
热议问题