Jig*_*tel 0 vb.net visual-studio-2010 visual-studio
我正在尝试在单个路径下的多个目录中搜索扩展名为“ .xml”和“ .pdf”的文件。
但这里的问题是目录由许多子目录组成,我必须读取由单个路径下的“ .xml”和“ .pdf”文件组成的子目录,如果子目录中缺少任何一个文件代码应该能够获得该特定的主目录名称。
请任何人帮我解决这个问题
任何帮助将不胜感激
小智 5
由于堆栈在 VB 中可用,我使用它们而不是递归,因为它易于调试并且递归例程失控的可能性较小。
Function SearchForFiles(ByVal RootFolder As String, ByVal FileFilter() As String) As List(Of String)
Dim ReturnedData As New List(Of String) 'List to hold the search results
Dim FolderStack As New Stack(Of String) 'Stack for searching the folders
FolderStack.Push(RootFolder) 'Start at the specified root folder
Do While FolderStack.Count > 0 'While there are things in the stack
Dim ThisFolder As String = FolderStack.Pop 'Grab the next folder to process
Try 'Use a try to catch any errors
For Each SubFolder In GetDirectories(ThisFolder) 'Loop through each sub folder in this folder
FolderStack.Push(SubFolder) 'Add to the stack for further processing
Next 'Process next sub folder
For Each FileExt In FileFilter 'For each File filter specified
ReturnedData.AddRange(GetFiles(ThisFolder, FileExt)) 'Search for and return the matched file names
Next 'Process next FileFilter
Catch ex As Exception 'For simplicity sake
End Try 'We'll ignore the errors
Loop 'Process next folder in the stack
Return ReturnedData 'Return the list of files that match
End Function
Run Code Online (Sandbox Code Playgroud)
确保包含Imports System.Io.Directory在源文件的顶部。调用很简单:
Dim Files = SearchForFiles("D:\Programming_VS\", {"*.xml", "*.pdf"})
Run Code Online (Sandbox Code Playgroud)
它返回包含搜索文件的字符串列表。
| 归档时间: |
|
| 查看次数: |
14207 次 |
| 最近记录: |