NPS
NPS

Reputation: 31

Recursive File Search in 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?

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.

Upvotes: 3

Views: 8871

Answers (3)

Chema
Chema

Reputation: 81

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.

Upvotes: 8

NPS
NPS

Reputation: 31

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.

Upvotes: 0

Hadi
Hadi

Reputation: 37313

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

Upvotes: 1

Related Questions