递归列出目录内容 [英] List content of directory recursively
本文介绍了递归列出目录内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要代码来从驱动器中获取目录的所有文件(包括所有子目录)并将其存储到数组中.稍后,我想使用该数组并搜索特定文件并删除该文件.
提前谢谢.
I need code to get all the files of a directory (including all subdirectories) from a drive and store it into an array. At a later time I want to use that array and search for particular file and delete that file.
Thanks in advance.
推荐答案
使用类Directory
(不是DirectoryInfo
),使用带有SearchOption
参数的方法GetFiles
:AllDirectories
用于递归目录搜索,TopDirectoryOnly
仅用于一个目录.
Use the classDirectory
(notDirectoryInfo
), the methodGetFiles
withSearchOption
parameter:AllDirectories
for recursive directory search,TopDirectoryOnly
for just one directory.
此页面应满足您的需求.它们显示了一个函数的代码,该函数以递归方式列出目录和所有子目录中的所有文件:
VB.NET中的递归目录功能 [
This page should cater to your needs. They show code for a function that recursively lists all files in directory and all sub directories:
Recursive directory function in VB.NET[^]
They return aList
, but you can turn that into an array with theList.ToArray()
method. I would not do that though because it''s so easy to search in a List using the Predicate delegate. Look that up on MSDN:List.Find
&List.FindAll
Please refer to SAKryukov''s answer, it is the best solution in this case as it relies solely on already built in .NET functions plus you get your files in an array as per your request (or requirement).
If you already looked upList.Find
andList.FindAll
and would like to use it, you can create a list from an array and then back again should the need arise:
List<String> myList = new List<String>(theArrayFromTheGetFilesCall);
希望对您有帮助!
最好的问候,
Manfred
Hope that helps you!
Best Regards,
Manfred
通过下面的链接[DirectoryInfo]
^ ]
互联网上有大量示例.发布问题前先在Google/CP中搜索
Go through the below link [DirectoryInfo]
Here[^]
There are thousnad of examples avaible in the internet., Search in Google/CP before posting a question
这篇关于递归列出目录内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文