列出目录+子目录中的所有文件和目录
问题描述:
我想列出目录中包含的每个文件和目录以及该目录的子目录。如果我选择C:\作为目录,该程序将获取其访问硬盘上每个文件和文件夹的名称。
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 the name of every file and folder on the hard drive that it had access to.
列表可能看起来像
fd\1.txt
fd\2.txt
fd\a\
fd\b\
fd\a\1.txt
fd\a\2.txt
fd\a\a\
fd\a\b\
fd\b\1.txt
fd\b\2.txt
fd\b\a
fd\b\b
fd\a\a\1.txt
fd\a\a\a\
fd\a\b\1.txt
fd\a\b\a
fd\b\a\1.txt
fd\b\a\a\
fd\b\b\1.txt
fd\b\b\a
答
String[] allfiles = System.IO.Directory.GetFiles("path/to/dir", "*.*", System.IO.SearchOption.AllDirectories);
其中 *。*
是匹配的模式文件
如果目录也需要,您可以这样:
If the Directory is also needed you can go like this:
foreach ( var file in allfiles){
FileInfo info = new FileInfo(file);
// Do something with the Folder or just add them to a list via nameoflist.add();
}