列出目录+子目录中的所有文件和目录 [英] List all files and directories in a directory + subdirectories

查看:289
本文介绍了列出目录+子目录中的所有文件和目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出目录中包含的每个文件和目录以及该目录的子目录。如果我选择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();
 }

这篇关于列出目录+子目录中的所有文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆