How to prevent Directory.GetFiles to "check" recycle bin and other "unsafe" places?

3.5k views Asked by At

So guys, I have a function in my application which to search for certain file in certain directory using GetFiles method

System.IO.Directory.GetFiles(string path, string searchPattern, System.IO.SearchOption)

It works fine, until when I choose drive directory (D:\ or C:\ and such) to be searched, because it's also accessing the Recycle Bin, and then restricted

Access to the path 'D:\$RECYCLE.BIN\S-1-5-21-106145493-3722843178-2978326776-1010' is denied.

It's also need to be able to search subfolders (SearchOption.AllDirectories) too.

How to SKIP such place to be searched? Because there may be any other folder which access also denied.

I capitalize SKIP because if I use try catch and an exception caught, then the entire search will also fail.

Thanks. Please clarify anything you need.

1

There are 1 answers

3
user1847129 On

EDITed for more clarity.

When recursively scanning a directory tree, say using a recursive method which takes the directory to start with as a parameter, you can get the attributes of the directory. Then check whether it's a system directory AND NOT a root directory like "C:\" - in that case you want to skip that directory, as it may be, for instance, the recycle bin.

Here's some code that does this, and also catches some common exceptions which occurred when I fiddled with directory scanning.

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);
    }
}