📜  拒绝访问路径“D:\$RECYCLE.BIN\S-1-5-18”. - 无论代码示例

📅  最后修改于: 2022-03-11 14:56:27.084000             🧑  作者: Mango

代码示例1
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;
    }
}