本文介绍了VB.NET中的递归文件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个功能可以对文件进行递归目录搜索,但是当我搜索驱动器时,出现访问被拒绝的错误,这将停止搜索.如何避免这些错误?
I have a function that does a recursive directory search for files but when I search a Drive I get Access denied errors which stops the search. How can I avoid these errors?
这是我使用的功能:
lstSearch = GetFilesRecursive(FolderBrowserDialogMain.SelectedPath)
Private Function GetFilesRecursive(ByVal path As String) As List(Of String)
Dim lstResult As New List(Of String)
Dim stkStack As New Stack(Of String)
stkStack.Push(path)
Do While (stkStack.Count > 0)
Dim strDirectory As String = stkStack.Pop
Try
lstResult.AddRange(Directory.GetFiles(strDirectory, "*.mp3"))
Dim strDirectoryName As String
For Each strDirectoryName In Directory.GetDirectories(strDirectory)
stkStack.Push(strDirectoryName)
Next
Catch ex As Exception
End Try
Loop
Return lstResult
End Function
感谢任何解决方案.
推荐答案
感谢代码,它可以正常工作,但是仔细查看后,我发现这行代码可以完成这项工作:
Thanks for the code, it worked, but after taking a closer look, i found this single line would do the job:
myfiles = IO.Directory.GetFiles(strpath, "*.*", IO.SearchOption.AllDirectories)
仅将搜索选项从TopDirectoryOnly更改为AllDirectories.我一直希望使用本机功能.
just changing the search option from TopDirectoryOnly to AllDirectories. I always look to use native functions.
这篇关于VB.NET中的递归文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!