|
-
Aug 25th, 2009, 09:22 PM
#1
Thread Starter
Junior Member
UnauthorizedAccessException
Hi.
I have a function that returns a value of the numbers of files, folders and subfolders in a directory. But i can't seem to handle the "UnauthorizedAccessException" which i get from certain read only files or folders. I tried to use both the DirectoryInfo.GetDirectories() and DirectoryInfo.GetFiles() methods. as you can see from the code below. This exception is thrown in start of the for each loop, so i'm not able to handel it with a try, Catch Ex As UnauthorizedAccessException to cintinue the loop.
Code Code:
Function CountFolders(ByVal Directory As String) As Integer
Dim FolderCount As Integer = 0
Dim SubDirectories As New ArrayList
Dim FolderInfo As DirectoryInfo = New DirectoryInfo(Directory)
For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
Continue For
End If
SubDirectories.Add(Dir.FullName)
FolderCount += 1
Next
For Each item As String In SubDirectories
FolderCount = CountFolders(item) + FolderCount
Next
Return FolderCount
End Function
How can i handle this exception and continue the loop.
-
Aug 25th, 2009, 09:30 PM
#2
Re: UnauthorizedAccessException
just wrap the loop in the try, catch ex as exception and run the code.
-
Aug 26th, 2009, 02:46 PM
#3
Thread Starter
Junior Member
Re: UnauthorizedAccessException
Hi formlesstree4, and thank you for your reply.
No, that will not work. As i explain in my post, to be able to continue the loop (Continue For), the try-catch needs to be inside the for loop. But the UnauthorizedAccessException happens before the loop begins. If i wrap the for loop in a try-catch, i will be able to handle the exception but not continue the loop. see examples below.
This will not work, as try-catch is outside the loop.
1 Code:
Try
For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
Continue For
End If
SubDirectories.Add(Dir.FullName)
FolderCount += 1
Next
Catch ex As Exception
Continue For ' Error : Continue For can only appear inside a For Statement
End Try
This will work, but it will not handle the exception.
2 Code:
For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
Try
If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
Continue For
End If
SubDirectories.Add(Dir.FullName)
FolderCount += 1
Catch ex As Exception
Continue For
End Try
Next
The exception is thrown in the "For Each Dir As DirectoryInfo In FolderInfo.GetDirectories" line.
Sorry if my english is a bit unreadable
Last edited by StianR; Aug 26th, 2009 at 02:54 PM.
-
Aug 26th, 2009, 03:57 PM
#4
Re: UnauthorizedAccessException
Well....You could populate an array first so you can handle that exception if it occurs, then do the loop, and control the inside from there.
Example
vb Code:
Try Dim DLists() As New String = FolderInto.GetDirectores.ToString 'Not sure if I can do this, but yeah. Catch Ex As UnauthorizedAccessException 'handle here End Try 'Now do the loop here, but I can't remember if you'll be able to access the DLists() Variable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|