• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

VB.Net Question

kzrssk

Member
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,
 
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.
 
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.
 
Back
Top