C# 如何防止Directory.GetFiles变为";勾选“;回收站和其他;“不安全”;地方?

C# 如何防止Directory.GetFiles变为";勾选“;回收站和其他;“不安全”;地方?,c#,io,directory,getfiles,C#,Io,Directory,Getfiles,伙计们,我在我的应用程序中有一个函数,可以使用这个方法搜索特定目录中的特定文件 它工作正常,直到我选择要搜索的驱动器目录(D:\或C:\等),因为它也在访问回收站,然后被限制 对路径“D:\$RECYCLE.BIN\S-1-5-21-106145493-3722843178-2978326776-1010”的访问被拒绝 它还需要能够搜索子文件夹(SearchOption.AllDirectories) 如何跳过这样的地方进行搜索?因为可能还有其他文件夹被拒绝访问 我将SKIP大写,因为如果我使用

伙计们,我在我的应用程序中有一个函数,可以使用这个方法搜索特定目录中的特定文件

它工作正常,直到我选择要搜索的驱动器目录(
D:\
C:\
等),因为它也在访问回收站,然后被限制

对路径“D:\$RECYCLE.BIN\S-1-5-21-106145493-3722843178-2978326776-1010”的访问被拒绝

它还需要能够搜索子文件夹(
SearchOption.AllDirectories

如何跳过这样的地方进行搜索?因为可能还有其他文件夹被拒绝访问

我将SKIP大写,因为如果我使用try-catch和异常捕获,那么整个搜索也将失败


谢谢。请澄清您需要的任何内容。

为了更清晰,请编辑

当递归地扫描目录树时,比如说使用一个以目录作为参数的递归方法,可以得到目录的属性。然后检查它是否是系统目录,而不是像“C:\”这样的根目录-在这种情况下,您希望跳过该目录,因为它可能是回收站

这里有一些代码可以做到这一点,还捕获了一些常见的异常,这些异常是在我处理目录扫描时发生的

void    scan_dir(string path)
{
    // Exclude some directories according to their attributes
    string[] files = null;
    string skipReason = null;
    var dirInfo = new DirectoryInfo( path );
    var isroot = dirInfo.Root.FullName.Equals( dirInfo.FullName );
    if (    // as root dirs (e.g. "C:\") apparently have the system + hidden flags set, we must check whether it's a root dir, if it is, we do NOT skip it even though those attributes are present
            (dirInfo.Attributes.HasFlag( FileAttributes.System ) && !isroot)    // We must not access such folders/files, or this crashes with UnauthorizedAccessException on folders like $RECYCLE.BIN
        )
    {   skipReason = "system file/folder, no access";
    }

    if ( null == skipReason )
    {   try
        {   files = Directory.GetFiles( path );
        }
        catch (UnauthorizedAccessException ex)
        {   skipReason = ex.Message;
        }
        catch (PathTooLongException ex)
        {   skipReason = ex.Message;
        }
    }

    if (null != skipReason)
    {   // perhaps do some error logging, stating skipReason
        return; // we skip this directory
    }

    foreach (var f in files)
    {   var fileAttribs = new FileInfo( f ).Attributes;
        // do stuff with file if the attributes are to your liking
    }

    try
    {   var dirs = Directory.GetDirectories( path );
        foreach (var d in dirs)
        {   scan_dir( d ); // recursive call
        }
    }
    catch (PathTooLongException ex)
    {   Trace.WriteLine(ex.Message);
    }
}

可能重复:请您发表评论?我的建议对我有效,我扫描了整个驱动器,这样就避免了根目录之外的系统目录。这就避免了为那些明确预期的情况抛出异常,这对我来说似乎更可取。(虽然在处理此类操作时,您显然应该捕获异常,但如果您能够首先阻止它们发生,我不会依赖它们,因为有些东西很容易检查)我确实意识到,老年退休金计划的问题有点陈旧,重复链接被张贴。我不太喜欢其他地方的回复,还有一个在这里,但最近被删除了,建议只依赖例外。因为我更喜欢防止明显预期的东西抛出异常(这可能会减慢您对数千个文件夹的扫描),所以我想我应该发布这个。我发现这个线索是因为几天前,我自己也在寻找解决这个问题的方法。找到了一个我比在别处看到的更喜欢的,于是回来了。
void    scan_dir(string path)
{
    // Exclude some directories according to their attributes
    string[] files = null;
    string skipReason = null;
    var dirInfo = new DirectoryInfo( path );
    var isroot = dirInfo.Root.FullName.Equals( dirInfo.FullName );
    if (    // as root dirs (e.g. "C:\") apparently have the system + hidden flags set, we must check whether it's a root dir, if it is, we do NOT skip it even though those attributes are present
            (dirInfo.Attributes.HasFlag( FileAttributes.System ) && !isroot)    // We must not access such folders/files, or this crashes with UnauthorizedAccessException on folders like $RECYCLE.BIN
        )
    {   skipReason = "system file/folder, no access";
    }

    if ( null == skipReason )
    {   try
        {   files = Directory.GetFiles( path );
        }
        catch (UnauthorizedAccessException ex)
        {   skipReason = ex.Message;
        }
        catch (PathTooLongException ex)
        {   skipReason = ex.Message;
        }
    }

    if (null != skipReason)
    {   // perhaps do some error logging, stating skipReason
        return; // we skip this directory
    }

    foreach (var f in files)
    {   var fileAttribs = new FileInfo( f ).Attributes;
        // do stuff with file if the attributes are to your liking
    }

    try
    {   var dirs = Directory.GetDirectories( path );
        foreach (var d in dirs)
        {   scan_dir( d ); // recursive call
        }
    }
    catch (PathTooLongException ex)
    {   Trace.WriteLine(ex.Message);
    }
}