Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get only all folders recursive

I was reading that once you find a exception trying to access to a directory the function interrupt and don’t continue with the next directory (at least that’s what i need). Âżany idea how can i workaround the problem?

I just looking for some folders to show..

C# .Net

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks advance

        static void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        try
        {
            System.IO.DirectoryInfo[] subDirs = null;

            // Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }
        catch (Exception)
        {

            //Do nothing
        } 
    }

>Solution :

You need to narrow the scope of your try catch to be inside the foreach loop

static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
    System.IO.DirectoryInfo[] subDirs = null;

    // Now find all the subdirectories under this directory.
    subDirs = root.GetDirectories();

    foreach (System.IO.DirectoryInfo dirInfo in subDirs)
    {
        // Resursive call for each subdirectory.
        try 
        {
            WalkDirectoryTree(dirInfo);
        }
        catch (Exception)
        {
        } 

    }
}

This will then catch the exception but continue with the next directory.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading