📅  最后修改于: 2022-03-11 14:56:27.084000             🧑  作者: Mango
private IEnumerable RecursiveFileSearch(string path, string pattern, ICollection filePathCollector = null)
{
try
{
filePathCollector = filePathCollector ?? new LinkedList();
var matchingFilePaths = Directory.GetFiles(path, pattern);
foreach(var matchingFile in matchingFilePaths)
{
filePathCollector.Add(matchingFile);
}
var subDirectories = Directory.EnumerateDirectories(path);
foreach (var subDirectory in subDirectories)
{
RecursiveFileSearch(subDirectory, pattern, filePathCollector);
}
return filePathCollector;
}
catch (Exception error)
{
bool isIgnorableError = error is PathTooLongException ||
error is UnauthorizedAccessException;
if (isIgnorableError)
{
return Enumerable.Empty();
}
throw error;
}
}