VB.Net Question

kzrssk

Member
Nov 13, 2005
111
0
0
Hi all,

I've got this block of code here:

Try
files = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
Catch ex As UnauthorizedAccessException

End Try

It will throw that UnauthorizedAccessException when it hits random folders I don't have access to (system-only folders, etc). What ends up happening, though, is that it just gives up and gets out of the Try-Catch (by design, I'm sure).

My question is: how do I make it keep going and continue to build the collection? Any help would be appreciated. Thanks all,
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
You need to break the search up, search top level folders, make a list of those you have access to and then continue. In the example shown even if you had access you could wind up with 10k+ if not 100k+ files on many systems, probably not something you want.

You could also write an interator over for/next and then do a foreach, you could filter out dirs you dont have access to in that interator.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
The key point here is that you need a higher-level control mechanism above the exception handling. As bsobel suggests a foreach loop, or some other construct that loops, adding a file to the list when there is no exception, and ignoring when there is one.