SearchOption.AllDirectories Acees to path is denied.

nineclicks

Member
Joined
Jun 13, 2013
Messages
18
Programming Experience
Beginner
VB.NET:
            For Each str As String In Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories)

                Dim fi As FileInfo = New FileInfo(str)
                If fi.Extension = ".ini" Then
                Else
                    fileList.Add(str)
                End If

            Next

I'm trying to get all of the files in a directory including sub-directories. Where I am filtering out .ini files I plan to implement a selectable blacklist or whitelist. Anyway the SearchOption.AllDirectories option gives me access denied errors on directories that I don't have access to of course. I simply want to ignore these inaccessible folders, but I don't know how without canceling the entire For loop. Is there a way around this without manually pulling all of the files?

Thanks.
 
That method offers no option to ignore inaccessible folders. You need to write your own recursive file search, e.g.
Private Function GetFiles(path As String, searchPattern As String) As String()
    Dim files As New List(Of String)

    Try
        'Get the files in the current folder.
        files.AddRange(Directory.GetFiles(path, searchPattern))

        'Get files in subfolders recursively.
        For Each subfolder In Directory.GetDirectories(path)
            files.AddRange(GetFiles(subfolder, searchPattern))
        Next
    Catch ex As UnauthorizedAccessException
        'Ignore inaccessible folder and continue.
    End Try

    Return files.ToArray()
End Function
 
I read a bit about recursive sub procedures, I hadn't realized how simple they were to write. I managed to get something similar worked out. Thanks.
 
I read a bit about recursive sub procedures, I hadn't realized how simple they were to write.
A recursive method is simply a method that calls itself. Beyond that, they are no different to any other method so can be as simple or as complex as the situation requires. As in any other scenario, you just have to get the logic worked out clearly first.
 
This should work for deferred processing...

    Friend Class SafeEnumerator
        Public Shared Function EnumerateFiles(strPath As String, strFileSpec As String, soOptions As SearchOption) As IEnumerable(Of String)
            Try
                Dim DirEnum = Enumerable.Empty(Of String)()
                If soOptions = SearchOption.AllDirectories Then
                    DirEnum = Directory.EnumerateDirectories(strPath).SelectMany(Function(x) EnumerateFiles(x, strFileSpec, soOptions))
                End If
                Return DirEnum.Concat(Directory.EnumerateFiles(strPath, strFileSpec, SearchOption.TopDirectoryOnly))
            Catch ex As UnauthorizedAccessException
                Return Enumerable.Empty(Of String)()
            End Try
        End Function
    End Class

    Private Sub Main() Handles Me.Load
        Dim NonIniFiles = From f In SafeEnumerator.EnumerateFiles("e:\", "*.*", SearchOption.AllDirectories)
                          Where Not f.ToUpper.Contains(".INI")

        For Each strFile In NonIniFiles
            ' ...
        Next
    End Sub
 
Back
Top