Results 1 to 3 of 3

Thread: [2005] getdirectories "access denied"

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    2

    [2005] getdirectories "access denied"

    How can I error handle ;

    Code:
    For each FLDR As String In my.computer.filesystem.getdirectories("C:\","*")
    ~ PROCESS FLDR ~ 
    Next
    when FLDR returns access denied

    THANKS!

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] getdirectories "access denied"

    Code:
    For Each FLDR As String In My.Computer.FileSystem.GetDirectories("D:\Setup", FileIO.SearchOption.SearchAllSubDirectories, "*")
                MessageBox.Show(FLDR)
            Next
    May you should try searching specific folders instead of the entire c drive which can have access restriction on certain folders (System folder for example)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] getdirectories "access denied"

    There is no way to handle that exception and complete the search. I think that is a big oversight with that method but you can only work with what you've got. If your folder tree may contain inaccessible folders then you have no choice but to perform your own recursive search. That way you can simply ignore an access denied exception and move on to the next folder:
    vb Code:
    1. Public Function GetDirectories(ByVal path As String) As List(Of String)
    2.     Dim subfolders As New List(Of String)
    3.  
    4.     For Each subfolder As String In IO.Directory.GetDirectories(path)
    5.         subfolders.Add(subfolder)
    6.  
    7.         Try
    8.             subfolders.AddRange(GetDirectories(subfolder))
    9.         Catch ex As UnauthorizedAccessException
    10.             'Ignore this folder and move on.
    11.         End Try
    12.     Next subfolder
    13.  
    14.     Return subfolders
    15. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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