Recursive File Search in VB.NET

。_饼干妹妹 提交于 2020-02-25 04:42:00

问题


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?

Here's the function I use:

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 any solutions.


回答1:


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)

just changing the search option from TopDirectoryOnly to AllDirectories. I always look to use native functions.




回答2:


You can achieve this by looping over files and directories recursively and adding some try catch logic.

Public Class MainClass

    Private Function GetAllFiles(ByVal strPath As String) As List(Of String)

        Dim lst As New List(Of String)

        GetFiles(strPath, lst)

        Return lst
    End Function


    Public Sub GetFiles(ByVal strpath As String, ByRef lstfiles As List(Of String))



        Try

            Dim str As String() = IO.Directory.GetFiles(strpath, "*.*", IO.SearchOption.TopDirectoryOnly)
            'Get Current Directory files
            lstfiles.AddRange(str)

            'Loop  over sub-directories
            For Each strDirectory As String In IO.Directory.GetDirectories(strpath, "*.*", IO.SearchOption.TopDirectoryOnly)


                Me.GetFiles(strDirectory, lstfiles)


            Next

        Catch ex As UnauthorizedAccessException
            'Access Denied exception

        Catch ex1 As Exception
            'Other exceptions

        End Try

    End Sub



End Class



回答3:


The only thing I changed to avoid the Access Denied errors was to use Try/Catch for UnauthorizedAccessException and then I just didn't handle it. When it's done searching use lstResult anyway you like. I have this code in a BackgroundWorker so it doesn't mess with the UI.

    Dim lstResult As New List(Of String)
    Dim stkStack As New Stack(Of String)
    stkStack.Push(SearchSelectedPath)
    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 UnauthorizedAccessException

        End Try
    Loop

I searched my C/D Drive's in about a minute and a half and found near 150 MP3's in each of the drives with no errors.



来源:https://stackoverflow.com/questions/44103640/recursive-file-search-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!