public static List<string> FindDirectories(string RootDirectory,
bool SearchAllDirectories, Predicate<string> Filter)
{
List<string> retList = new List<string>();
try
{
// create a directory info object
DirectoryInfo di = new DirectoryInfo(RootDirectory);
// loop through directories populating the list
Parallel.ForEach(di.GetDirectories(), folder =>
{
try
{
// add the folder if it passes the filter
if ((Filter == null) || (Filter(folder.FullName)))
{
// add the folder
retList.Add(folder.FullName);
// get it's sub folders
if (SearchAllDirectories)
retList.AddRange(FindDirectories(folder.FullName, true,
Filter));
}
}
catch (UnauthorizedAccessException)
{
// don't really need to do anything
// user just doesn't have access
}
catch (Exception ex)
{
// TODO: log the exception
}
});
}
catch (Exception ex)
{
// TODO: save exception
}
// return the list
return retList;
}