List all files and directories in a directory + subdirectories

前端 未结 15 1905
陌清茗
陌清茗 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:28

    Directory.GetFileSystemEntries exists in .NET 4.0+ and returns both files and directories. Call it like so:

    string[] entries = Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories);
    

    Note that it won't cope with attempts to list the contents of subdirectories that you don't have access to (UnauthorizedAccessException), but it may be sufficient for your needs.

提交回复
热议问题