通过目录结构解析时的System.StackOverflow异常 [英] System.StackOverflow Exception while Parsing through Directory Structure
问题描述
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屋!