public static List<string> FindFiles(string RootDirectory,
bool SearchAllDirectories, Predicate<string> Filter)
{
List<string> retList = new List<string>();
try
{
// get the list of directories
List<string> DirList = new List<string> { RootDirectory };
// get sub directories if allowed
if (SearchAllDirectories)
DirList.AddRange(FindDirectories(RootDirectory, true, null));
// loop through directories populating the list
Parallel.ForEach(DirList, folder =>
{
// get a directory object
DirectoryInfo di = new DirectoryInfo(folder);
try
{
// loop through the files in this directory
foreach (FileInfo file in di.GetFiles())
{
try
{
// add the file if it passes the filter
if ((Filter == null) || (Filter(file.FullName)))
retList.Add(file.FullName);
}
catch (Exception ex)
{
// TODO: log the exception
}
}
}
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;
}