IO.SearchOption.AllDirectories Exception...

runnindave

New member
Joined
Jul 31, 2007
Messages
1
Programming Experience
Beginner
Hey, my first post.

New to VB.net and loving it so far. My issue is that I have a simple app that looks at all directories on a server and reports information on each file in each directory.

The problem is some servers have defective directories that cause the following code to throw a "DirectoryNotFoundException"

VB.NET:
'-----------------------get all files from path
            Dim Directory As New IO.DirectoryInfo("" & dirPath & "")
            Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.*", IO.SearchOption.AllDirectories)
            Dim singleFile As IO.FileInfo

When I use a TRY- CATCH - END TRY I can capture the error.

What I need to do is if this defective directory pops up then somehow delete it or simply move on to the next directory.

I used a ON ERROR RESUME NEXT and it will not move on to the next directory and finish the code.

Thanks for any help

Dave
 
The call to GetFiles either succeeds or it doesn't. If an inaccessible folder is encountered along the way then the whole thing fails. You should only be using GetFiles with AllDirectories if you know that you will not encounter an inaccessible folder. Otherwise you need to implement your own recursive file search, in which case you can catch exceptions for individual folders and just continue.
 
Back
Top