私が達成しようとしているのは、ファイルを解析して、特定のドライブまたはフォルダー構造内のすべてのファイルのリストを取得することです。また、保護されたファイルの場合に発生する未承認の例外を処理しようとしています。コードはほとんどの場合正常に動作しますドライブとフォルダーですが、Windows ドライブ (C:) のような場合には、System.StackOverflow 例外がスローされます。何が問題なのですか?もっと良い方法はありますか?
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);
}
}
}
}