如何输出目录层次结构中的每个文件和文件夹
我正在寻找一种访问所有文件和文件夹以及这些文件夹中的文件的方法。
举个例子。
如果我有的话,
- Root
+ file1
+ file2
-- Directory
-+ file1
-+ file2
---Directory
--+ file1
--+ file2
我希望能够输出程序中的每个文件,这样它就会像:
root/file1
root/file2
root/Directory/file1
root/Directory/file2
root/Directory/Directory/file1
root/Directory/Directory/file2
与文件夹层次结构匹配。
有办法做到这一点吗?
谢谢
I'm looking for a way to all files and folders and files in those folders.
As an example.
if I have
- Root
+ file1
+ file2
-- Directory
-+ file1
-+ file2
---Directory
--+ file1
--+ file2
I would like to be able to output each one of those files in the program so it would be like:
root/file1
root/file2
root/Directory/file1
root/Directory/file2
root/Directory/Directory/file1
root/Directory/Directory/file2
As matching to the folder hierarchy.
Is there anyway to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下示例完全符合您的要求:-
http://www.dotnetperls.com/recursively-find -文件
The following example does exactly what you want:-
http://www.dotnetperls.com/recursively-find-files
使用 System.IO 中的 DirectoryInfo 类、FileInfo 类来获取这些详细信息。
use DirectoryInfo class,FileInfo classes inside your System.IO to get these details.
使用 Linq 进行某些提升
会丢失子字符串以获取完整路径。或者,如果您只想要根对象的文件夹名称(例如,如果您将 c:\users\bob\fish 作为目录,并且您只想要fish\foldername,则您将执行以下操作..
如果您标记额外的
.Select(f=>f.Replate(@"\","/"))
在语句末尾,您可以使用 / 作为路径分隔符而不是 \Using Linq for some of the lifting
miss out the substring to get the full path. Or if you just want the folder name of the root object (eg if you did c:\users\bob\fish as the directory and you just wanted fish\foldername you would do the following..
if you tag an extra
.Select(f=>f.Replate(@"\","/"))
on the end of the statement you can use / as the path seperator instead of \您可以使用 System.IO.Directory.GetFiles(..,..,System.IO.SearchOption.AllDirectories) 返回文件数组和完整路径名,然后打印该数组。如果您需要排序,您可以递归循环目录并在每个目录上使用 System.IO.Directory.GetFiles(..,.,System.IO.SearchOption.TopDirectoryOnly) 。
you could use System.IO.Directory.GetFiles(..,..,System.IO.SearchOption.AllDirectories) which returns an array of files and full path names and then print that array. if you need sorting you could recursively loop through directories and use System.IO.Directory.GetFiles(..,..,System.IO.SearchOption.TopDirectoryOnly) on each directory instead.