0

What im trying to achieve is,Get the list of all files in a specific drive or Folder Structure by parsing through the Files.I'm also trying to handle the Unauthorized Exception which occures in case of protected files.The Code works fine in most drives and folders but in some cases like the Windows Drive(C:),A System.StackOverflow EXception is thrown.What could be the problem?Is there a better way to do it?

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

        // First, process all the files directly under this folder
        try
        {
            files = root.GetFiles("*.*");
        }
        // This is thrown if even one of the files requires permissions greater
        // than the application provides.
        catch (UnauthorizedAccessException e)
        {
           //eat
        }

        catch (System.IO.DirectoryNotFoundException e)
        {
           //eat
        }

        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {

                Console.WriteLine(fi.FullName);
            }

            // 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);
            }
        }            
    }
}
techno
  • 6,100
  • 16
  • 86
  • 192
  • 2
    If you just want to list all files anywhere in the directory tree, you can use `root.GetFiles("*.*", SearchOption.AllDirectories)`. This way you don't need to recurse. – porges Jun 23 '12 at 05:12
  • @Porges I know this Method,Im doing this process because my Application does not have Admin Privileges. – techno Jun 23 '12 at 05:14
  • @techno, if your app doesn't have admin privileges, how does doing it recursively go around the permission problem? – Ray Cheng Jun 23 '12 at 05:32
  • @Ray Cheng It will not process protected files. – techno Jun 23 '12 at 05:34
  • @techno, can you process protected files with recursion? – Ray Cheng Jun 23 '12 at 05:42
  • @RayCheng No.That's why im ignoring it.Don't you get the point of the question? – techno Jun 23 '12 at 05:50
  • @techno, now I get it. You can't use `Getfiles` because it throws exception so you try to do it with recursion. I found this question is trying to get a list of files that the running process has permission to. Please give it a try. http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure – Ray Cheng Jun 23 '12 at 06:08
  • I copied your function and executed it with `WalkDirectoryTree(new System.IO.DirectoryInfo(@"C:\"));`. It completed just fine, having recursed to a maximum depth of 27, having found 433,251 files with a maximum path length of 264. I don't think recursion is your problem. – Gabe Jun 30 '12 at 05:23
  • @Gabe The same code causes SO Exception in my case.my C DRIVE is populated with more than 90GB of data. – techno Jun 30 '12 at 05:32
  • What's the last thing that gets printed before the SO? When you run it in a debugger, how many instances of `WalkDirectoryTree` stack frames are there when it overflows? – Gabe Jun 30 '12 at 05:39

1 Answers1

3

Have you tried stepping through with a debugger to see what is happening?

Sounds like recursion, maybe there is a NTFS Junction Point somewhere that is pointing to a higher level.

The definition of a StackOverflowException according to MSDN is

The exception that is thrown when the execution stack overflows because it contains too many nested method calls. This class cannot be inherited.

So that's why I'm guessing that. It is unlikely that your directory structure on your system is deeper than the number of calls the execution stack allows.

JeremyWeir
  • 24,118
  • 10
  • 92
  • 107