Click here to Skip to main content
15,899,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add all files from C:\ to the listbox that exist in directories and subdirectories but the program fails when an access denied file is detected.

the code I have so far is: :)

Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim startup_path As String = Application.StartupPath
  txtDir.Text = startup_path.Substring(0, startup_path.LastIndexOf("\"))
        txtDir.Select(0, 0)
    End Sub

Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        Get the pattern without stuff in parens.
        Dim pattern As String = cboPattern.Text
        If pattern.IndexOf("(") >= 0 Then
            pattern = pattern.Substring(0, pattern.IndexOf("("))
        End If

        lstFiles.Items.Clear()
        Dim dir_info As New DirectoryInfo(txtDir.Text)
        ListFiles(lstFiles, pattern, dir_info)
    End Sub

' Add the files in this directory's subtree to the ListBox.
Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
 ' Get the files in this directory.
        Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
        For Each fs_info As FileInfo In fs_infos
        lstFiles.Items.Add(fs_info.FullName)
        Next fs_info
        fs_infos = Nothing
 ' Search subdirectories.
        Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
        For Each subdir As DirectoryInfo In subdirs
            ListFiles(lst, pattern, subdir)
        Next subdir
    End Sub
End Class


Where in the code can I add a try catch statement or something that will allow the program to skip the access denied file and go to the next?
Posted
Updated 3-Dec-10 18:02pm
v3
Comments
Dr.Walt Fair, PE 4-Dec-10 0:02am    
[Added <pre> tags to code for readability.]

I'd step through the code to confirm what exact exception type you are getting, but the solution to ignore the inaccessible files should be something like the following. (I didn't test this, so use it as a guide and double check the exception type, etc.)

VB
' Get the files in this directory.
    Dim fs_infos() As FileInfo
Try
    fs_infos = dir_info.GetFiles(pattern)
    For Each fs_info As FileInfo In fs_infos

    ... etc ...

Catch ex As AccessDeniedException
    ' No need to do anything, just ignore
End Catch
End Try
 
Share this answer
 
Comments
Dale 2012 4-Dec-10 0:50am    
Ok I have tried what you have said along with a few others ways to no avail. Also there is no catch statement "accessdeniedexception"

This is the code I have now:

Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
' Get the files in this directory.
Try
Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
lstFiles.Items.Add(fs_info.FullName)
Next fs_info
fs_infos = Nothing
' Search subdirectories.
Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
For Each subdir As DirectoryInfo In subdirs
ListFiles(lst, pattern, subdir)
Next subdir
Catch ex As AccessViolationException
End Try
End Sub


This is still not working :(
Dale 2012 4-Dec-10 1:23am    
thank you for your help but I figured it out now......

the correct code is:

' Add the files in this directory's sub tree to the ListBox.
Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
' Get the files in this directory.
Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
lstFiles.Items.Add(fs_info.FullName)
Next fs_info
fs_infos = Nothing
' Search subdirectories.
Try
Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
For Each subdir As DirectoryInfo In subdirs
ListFiles(lst, pattern, subdir)
Next subdir
Catch ex As Exception
End Try
End Sub

Just for anyone wondering.......
Dr.Walt Fair, PE 4-Dec-10 1:35am    
I don't like to use "Catch ex As Exception", because it catches every exception - it's too general. You are expecting that the access exception can happen and are willing to accept that, so it's fair game to catch it. But what happens if you get another exception? Using "Catch ex As Exception" you'd play h*ll troubleshooting that or worse, you'd never know if there was a problem.

See my other solution that traps the "UnauthorizedAccessException" error correctly.
Yeah, the exception is UnauthorizedAccessException, which is why I recommended that you should test it. There are also 2 places where the exception can happen, so you need to trap them both separately. Try this; it works on my system:

Private Sub ListFiles(ByVal lst As ListBox,
        ByVal pattern As String, ByVal dir_info As DirectoryInfo)
    ' Get the files in this directory.
    Dim fs_infos() As FileInfo
    Try
        fs_infos = dir_info.GetFiles(pattern)
        For Each fs_info As FileInfo In fs_infos
            lst.Items.Add(fs_info.FullName)
        Next fs_info
    Catch ex As UnauthorizedAccessException
    End Try
    fs_infos = Nothing
    ' Search subdirectories.
    Dim subdirs() As DirectoryInfo
    subdirs = dir_info.GetDirectories()
    Try
        For Each subdir As DirectoryInfo In subdirs
            ListFiles(lst, pattern, subdir)
        Next subdir
    Catch ex As UnauthorizedAccessException
    End Try
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900