I am using C# 4.5 and I’m trying to scan a fileshare for all folders. I want to skip any folder I cannot access and just continue. What I have done is to do it recursively, this throws a stackoverflow. I understand why this happens. So my question is: are there any work arounds?
How would you implemented this since we cannot use recursive search? Are there any third-party libraries I can use to simplify this? GetFolder function is only extracting some information and return a custom class, this works fine.
public void GetFoldersFromFS(string filePath)
{
if (filePath == null)
{
return;
}
Directory.SetCurrentDirectory(filePath);
try
{
foreach (var directory in Directory.EnumerateDirectories(Directory.GetCurrentDirectory()))
{
Resources.Add(GetFolder(new DirectoryInfo(directory)));
GetFoldersFromFS(directory);
}
}
catch (UnauthorizedAccessException e)
{
Log.Warn(e.Message);
}
catch (PathTooLongException e)
{
Log.Warn(e.Message);
}
}
Voila, scanning without recursion.
Pseudo code (without any try-catch):