通过目录结构解析时的System.StackOverflow异常 [英] System.StackOverflow Exception while Parsing through Directory Structure

查看:179
本文介绍了通过目录结构解析时的System.StackOverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是,通过解析文件获取特定驱动器或文件夹结构中的所有文件的列表。我也尝试处理在受保护的文件的情况下发生的未经授权的异常。代码工作在大多数驱动器和文件夹中都可以使用,但在某些情况下,如Windows驱动器(C :),则会抛出一个System.StackOverflow EXception。可能是什么问题?是否有更好的方法?

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

//首先,直接在此文件夹下处理所有文件
尝试
{
files = root.GetFiles(*。*);
}
//如果甚至其中一个文件需要的权限大于应用程序提供的
//,则抛出这个错误。
catch(UnauthorizedAccessException e)
{
//吃
}

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

如果(files!= null)
{
foreach(文件中的System.IO.FileInfo fi)
{

Console.WriteLine(fi.FullName);
}

//现在找到此目录下的所有子目录。
subDirs = root.GetDirectories();

foreach(subDirs中的System.IO.DirectoryInfo dirInfo)
{
//每个子目录的Resursive调用。
WalkDirectoryTree(dirInfo);
}
}
}
}


解决方案

您是否尝试过调试器,看看发生了什么?



听起来像递归,也许有一个 NTFS Junction Point 指向更高级别的地方。



根据MSDN 的StackOverflowException的定义是


执行堆栈溢出
时抛出异常,因为它包含太多的嵌套方法调用。这个类不能被
继承。


所以这就是为什么我猜这个。您的系统上的目录结构不太可能比执行堆栈允许的调用次数更深。


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);
            }
        }            
    }
}

解决方案

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.

这篇关于通过目录结构解析时的System.StackOverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆