Recursive file search fails when reaching access denied folder-VBForums
Results 1 to 6 of 6

Thread: Recursive file search fails when reaching access denied folder

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    Recursive file search fails when reaching access denied folder

    I'm trying to retrieve a list of files from a specific folder, which has plenty of sub folder and files. The problem is that some of this folders, cannot be accessed due to access denied.

    This is my first solution to search for the files:

    Code:
    public void GetFiles(string dir)
            {
                foreach (string d in Directory.GetDirectories(dir))
                {
                    foreach (string f in Directory.GetFiles(d))
                    {
                        //do something with the file
                    }
                    GetFiles(d);
                }
            }
    Using this I got an exception unhandled, I had to add a Try...Catch statement:

    Code:
    public void GetFiles(string dir)
            {
                try
                {
                    foreach (string d in Directory.GetDirectories(dir))
                    {
                        foreach (string f in Directory.GetFiles(d))
                        {
                            //do something with the file
                        }
                        GetFiles(d);
                    }
                }   
                catch (Exception)
                {
                    
                }
            }
    The problem with this is that as soon as I reach a access denied folder I will get into the catch and the recursive search will stop. I figured the best way to build this method is to check whether the folder is able to be read or not.

    There is any way to check to see if the folder can be read? Also, if someone has a better solution for my problem it would be nice to check what you got in mind.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Recursive file search fails when reaching access denied folder

    You could put the exception handler within the loop instead of wrapping the whole loop with it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    685

    Re: Recursive file search fails when reaching access denied folder

    Understand that when an exception is caught, that effectively breaks you out of the loop stopping any iteration you were doing through the filesystem.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    VB.NET Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Recursive file search fails when reaching access denied folder

    No....you wrap it in the inner loop so it skips folders it can't read.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    685

    Re: Recursive file search fails when reaching access denied folder

    Quote Originally Posted by Niya View Post
    No....you wrap it in the inner loop so it skips folders it can't read.
    I'm letting him know why it stops the iteration... It's basically like a jmp instruction out of the scope of the iteration from the loop.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    VB.NET Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Recursive file search fails when reaching access denied folder

    Oh forgive me. Didn't realize you were addressing the OP.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.