Results 1 to 4 of 4

Thread: UnauthorizedAccessException

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    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:
    1. Function CountFolders(ByVal Directory As String) As Integer
    2.  
    3.         Dim FolderCount As Integer = 0
    4.         Dim SubDirectories As New ArrayList
    5.         Dim FolderInfo As DirectoryInfo = New DirectoryInfo(Directory)
    6.  
    7.         For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
    8.             If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
    9.                 Continue For
    10.             End If
    11.             SubDirectories.Add(Dir.FullName)
    12.             FolderCount += 1
    13.         Next
    14.  
    15.         For Each item As String In SubDirectories
    16.             FolderCount = CountFolders(item) + FolderCount
    17.         Next
    18.  
    19.         Return FolderCount
    20.  
    21.     End Function

    How can i handle this exception and continue the loop.

  2. #2

    Re: UnauthorizedAccessException

    just wrap the loop in the try, catch ex as exception and run the code.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    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:
    1. Try
    2.             For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
    3.                 If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
    4.                     Continue For
    5.                 End If
    6.                 SubDirectories.Add(Dir.FullName)
    7.                 FolderCount += 1
    8.             Next
    9.         Catch ex As Exception
    10.             Continue For ' Error : Continue For can only appear inside a For Statement
    11.         End Try


    This will work, but it will not handle the exception.
    2 Code:
    1. For Each Dir As DirectoryInfo In FolderInfo.GetDirectories
    2.             Try
    3.                 If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then
    4.                     Continue For
    5.                 End If
    6.                 SubDirectories.Add(Dir.FullName)
    7.                 FolderCount += 1
    8.             Catch ex As Exception
    9.                 Continue For
    10.             End Try
    11.         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.

  4. #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:
    1. Try
    2. Dim DLists() As New String = FolderInto.GetDirectores.ToString 'Not sure if I can do this, but yeah.
    3. Catch Ex As UnauthorizedAccessException
    4. 'handle here
    5. End Try
    6. '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
  •  



Click Here to Expand Forum to Full Width