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
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.