List all files and directories in a directory + subdirectories

前端 未结 15 1891
陌清茗
陌清茗 2020-12-13 01:46

I want to list every file and directory contained in a directory and subdirectories of that directory. If I chose C:\\ as the directory, the program would get every name of

15条回答
  •  死守一世寂寞
    2020-12-13 02:36

    I am afraid, the GetFiles method returns list of files but not the directories. The list in the question prompts me that the result should include the folders as well. If you want more customized list, you may try calling GetFiles and GetDirectories recursively. Try this:

    List AllFiles = new List();
    void ParsePath(string path)
    {
        string[] SubDirs = Directory.GetDirectories(path);
        AllFiles.AddRange(SubDirs);
        AllFiles.AddRange(Directory.GetFiles(path));
        foreach (string subdir in SubDirs)
            ParsePath(subdir);
    }
    

    Tip: You can use FileInfo and DirectoryInfo classes if you need to check any specific attribute.

提交回复
热议问题