递归搜索问题 [英] Recursive Search problem
本文介绍了递归搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下函数执行递归搜索并将所有文件添加到列表框中。但是当我点击没有扩展名的文件时,我收到错误。我不能在DVD上添加扩展名。有谁知道我怎么解决这个问题
这是函数
私有 功能 RecursiveSearch( ByVal 路径作为 字符串)作为 Boolean
Dim dirInfo As 新 IO.DirectoryInfo(路径)
Dim fileObject As FileSystemInfo
对于 每个 fileObject 在 dirInfo.GetFileSystemInfos()
如果(fileObject.Attributes 和 FileAttributes.Director y)= FileAttributes.Directory 然后
RecursiveSearch(fileObject.FullName)
Else
我 .lstDirectory.Items.Add(fileObject.FullName)
结束 < span class =code-keyword>如果
下一步
返回 True
结束 功能
解决方案
为什么不使用非递归函数 Directory.GetFiles [ ^ ]
昏暗文件作为 字符串()= Directory.GetFiles(path, *。*,SearchOption.AllDirectories)如果需要,您可以使用AddRange将它们全部添加到列表中。
Hi,
I have the following function that does a recursive search and adds all files to a list box. But when I hit a file that has no extension I get an error. I can''t add the extension as its on a DVD. Does anyone know how I can solve this
Here is the function
Private Function RecursiveSearch(ByVal path As String) As Boolean
Dim dirInfo As New IO.DirectoryInfo(path)
Dim fileObject As FileSystemInfo
For Each fileObject In dirInfo.GetFileSystemInfos()
If (fileObject.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
RecursiveSearch(fileObject.FullName)
Else
Me.lstDirectory.Items.Add(fileObject.FullName)
End If
Next
Return True
End Function
解决方案
Why not just use the non-recursive function Directory.GetFiles[^]
Dim files As String() = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)You can then add them all to your list with AddRange, if you need to.
这篇关于递归搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文