Results 1 to 7 of 7

Thread: Search files (access denied)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    203

    Search files (access denied)

    I've tried various methods to search for files, but each time I encounter a "acces denied" error, which stops the search.

    Is there a way to scan for files, and skip the files it doesn't have access to, or force access to it?

    I want to gather all exe's on a specific drive.

    This works, when there are no restricted files/paths

    Dim searchResults As String() = Directory.GetFiles(cboDrives.Text, "*.exe", SearchOption.AllDirectories)

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Search files (access denied)

    you'll have to use foreach loop and try catch block
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    203

    Re: Search files (access denied)

    I tried
    Code:
     Dim searchResults As String() = Directory.GetFiles(cboDrives.Text, "*.exe", SearchOption.AllDirectories)
            For Each File In searchResults
                Try
                    ' MsgBox("")
                    'HASHCheck(File)
                    lblFile.Text = File
                    lstFiles.Items.Add(File)
                    countfiles += 1
                    lblStatusSearch.Text = "Start a scan for files on your drive (" & countfiles & ") in " & time & " seconds"
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                    Continue For
                End Try
    
            Next
    If I place
    Code:
    Dim searchResults As String() = Directory.GetFiles(cboDrives.Text, "*.exe", SearchOption.AllDirectories)
    inside the try, it stop searching when it encounters a access denied

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Search files (access denied)

    Hows about search forums search+denied

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    203

    Re: Search files (access denied)

    I'm stuck on this line

    Code:
    Try
                    MyFiles.AddRange(IO.Directory.GetFiles(Path))
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
    It says it can't get to C:\Documents and settings...

    I tried changing the UAC to this, but it didn't work
    Code:
    <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />
    Can I make it skip the path it doesn't have access to?
    Because it completely stops searching after it encounters the access denied.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Search files (access denied)

    The link I gave shows an example of an app that does everything you need. Your going to show more of the code you have modified for anyone to advise you. Where is the loop?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    203

    Re: Search files (access denied)

    This is the complete search code

    Code:
    Public Class FileSearcher
        Dim Dirs As Boolean
        Dim SString As String
        Dim FilesFound As New Specialized.StringCollection
        Private Const IllegalDir As String = "System Volume Information"
        Public Sub New(ByVal StartPath As String, ByVal SearchString As String, Optional ByVal Dirs_Included As Boolean = False)
            Dirs = Dirs_Included
            SString = SearchString
            Call RecursiveSearch(StartPath)
        End Sub
    
        Private Sub RecursiveSearch(ByVal Path As String)
            
            Try
                Dim MyFiles As New Specialized.StringCollection
                Try
                    MyFiles.AddRange(IO.Directory.GetFiles(Path))
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
    
    
                If Me.Dirs = True Then MyFiles.AddRange(IO.Directory.GetDirectories(Path))
                For i As Int32 = 0 To MyFiles.Count - 1
                    Try
                        If MyFiles.Item(i).EndsWith("exe") Then
                            FilesFound.Add(MyFiles.Item(i))
                        End If
                    Catch ex As Exception
                        Continue For
                    End Try
    
                Next
            Catch ex As Exception
                'Illegal Directory
                MessageBox.Show("I can't seem to access some folders, are you running this as administrator?" & vbNewLine _
    + "If not, right click the application/shortcut and select 'Run as adminstrator'" _
    & vbNewLine + "When prompted for permission, choose 'Yes'" & vbNewLine + _
    "It's also possible that the given drive doesn't exist" & vbNewLine & _
    ex.Message, "Error")
    
            Finally
                If IO.Directory.Exists(Path) Then
    
                    Try
                        For Each Dir As String In IO.Directory.GetDirectories(Path)
                            If IO.Path.GetFileName(Dir) <> IllegalDir Then Call RecursiveSearch(Dir)
                        Next
                    Catch ex As Exception
    
                    End Try
    
                Else
                End If
            End Try
        End Sub
        Public ReadOnly Property Results() As Specialized.StringCollection
            Get
                Return FilesFound
            End Get
        End Property
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width