My.Computer.FileSystem.FindInFiles - UnauthorizedAccessException - DirectorySecurity

JasonWMcB

New member
Joined
Jul 18, 2007
Messages
4
Location
Grand Rapids, MI
Programming Experience
Beginner
Hello everyone,

Thank you in advance for any help you can give me.

I am trying to use the My.Computer.FileSystem.FindInFiles methode to locate a file on my system. I can not seem to get around an UnathorizedAccessException exception. I am currently trying to step through my directory structure and determine if I (the running process) has access to the folder before attempting to perform a FindInFiles method against it.

I am currently having trouble on how to determine if I (the running process) has FileSystem.FullAccess rights to the directory.

Any suggestions on my current issue or my overall approch?

Thanks.
 
VB.NET:
        Try
            My.Computer.FileSystem.FindInFiles(...)
        Catch ex As Exception
            'problem occurred
        End Try
 
When I try that the entire method fails and I get no return. I should have added that I am begining my search at "C:\" and I am getting hung up on system folders such as 'C:\System Volume Information'.
 
Last edited:
Try this:
VB.NET:
Function FindInFiles(ByVal folder As String, _
                     ByVal search As String, _
                     ByVal caseSensitive As Boolean, _
                     ByVal ParamArray wildcards() As String) _
As Collections.ObjectModel.ReadOnlyCollection(Of String)

    Dim returns As New List(Of String)
    returns.AddRange(My.Computer.FileSystem.FindInFiles(folder, search, caseSensitive, _
                                            FileIO.SearchOption.SearchTopLevelOnly, wildcards))
    For Each subfolder As String In IO.Directory.GetDirectories(folder)
        Try
            returns.AddRange(My.Computer.FileSystem.FindInFiles(subfolder, search, caseSensitive, _
                                            FileIO.SearchOption.SearchAllSubDirectories, wildcards))
        Catch ex As Exception
            'problem with subfolder
        End Try
    Next
    Return New Collections.ObjectModel.ReadOnlyCollection(Of String)(returns)
End Function
For example:
VB.NET:
ListBox1.DataSource = FindInFiles("c:\", "studio", False, "*.txt")
 
Thank you so much for your help. Your code worked great for the example scenerio I gave. I still had problems with some folders that were burried deep into the system (ex. C:\Documents and Settings\UserID\Local Settings\Temp\......). I was able to take your code example and put together the following snippet that works with subfolders having access issues.

VB.NET:
    Function FindInFiles(ByVal folder As String, _
                        ByVal search As String, _
                        ByVal caseSensitive As Boolean, _
                        ByVal recursive As FileIO.SearchOption, _
                        ByVal ParamArray wildcards() As String) _
                            As Collections.ObjectModel.ReadOnlyCollection(Of String)

        Dim returns As New List(Of String)

        Try
            returns.AddRange(My.Computer.FileSystem.FindInFiles(folder, search, caseSensitive, FileIO.SearchOption.SearchTopLevelOnly, wildcards))
        Catch ex As Exception
            'problem with subfolder
        End Try

        If recursive = FileIO.SearchOption.SearchAllSubDirectories Then
            Try
                For Each subfolder As String In IO.Directory.GetDirectories(folder)
                    returns.AddRange(FindInFiles(subfolder, search, caseSensitive, recursive, wildcards))
                Next
            Catch ex As Exception
                'problem with subfolder
            End Try
        End If

        Return New Collections.ObjectModel.ReadOnlyCollection(Of String)(returns)
    End Function

Thanks again for your help.
 
Excellent, recursion is the way to go in this case, I didn't put very much thought and time into the previous post, also I was running as admin so I didn't think about limited access to other subfolders. (that was the excuse:)) Now one improvement is necessary, there is no point in accessing the subfolders of a folder that already have raised access exception, so move the recursion block into the first Try block and remove the extra level Try.
 
Thanks JohnH. I hope to be as smart as you some day. For anyone that cares, here is the updated code snippet.

VB.NET:
    Function FindInFiles(ByVal folder As String, _
                        ByVal search As String, _
                        ByVal caseSensitive As Boolean, _
                        ByVal recursive As FileIO.SearchOption, _
                        ByVal ParamArray wildcards() As String) _
                            As Collections.ObjectModel.ReadOnlyCollection(Of String)

        Dim returns As New List(Of String)

        Try
            returns.AddRange(My.Computer.FileSystem.FindInFiles(folder, search, caseSensitive, FileIO.SearchOption.SearchTopLevelOnly, wildcards))
            If recursive = FileIO.SearchOption.SearchAllSubDirectories Then
                Try
                    For Each subfolder As String In IO.Directory.GetDirectories(folder)
                        returns.AddRange(FindInFiles(subfolder, search, caseSensitive, recursive, wildcards))
                    Next
                Catch ex As Exception
                    'problem with subfolder
                End Try
            End If
        Catch ex As Exception
            'problem with subfolder
        End Try

        Return New Collections.ObjectModel.ReadOnlyCollection(Of String)(returns)
    End Function
 
remove the extra level Try and it should be good to go, makes the code cleaner. ;)
 
Back
Top