2

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.

Moses Aprico
  • 1,951
  • 5
  • 30
  • 53
  • 1
    possible duplicate: http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure?rq=1 – Maverik Nov 19 '14 at 13:12

1 Answers1

3

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);
    }
}
user1847129
  • 1,010
  • 2
  • 9
  • 21
  • Could downvoters please comment? My suggestion works for me, I scanned my whole drive and avoided system dirs besides root that way. This prevented exceptions from being thrown for those clearly expected cases, which seems preferable to me. – user1847129 Jul 01 '16 at 18:10
  • (Although you obviously should catch exceptions when dealing with such operations, but I wouldn't rely on them if you can prevent them to occor in the first place because somethign is easy to check) – user1847129 Jul 01 '16 at 18:16
  • I do realize that the OPs question is somewhat old and duplicate links were posted. I wasn't too fond of the replies elsewhere and another one that was here but got deleted recently suggesting to solely rely on exceptions. Since I prefer to prevent clearly expected things from throwing exceptions (which could slow down your scanning of thousands of folders), I thought I'd post this. I discovered this thread because some days ago, I was looking for a solution to that problem myself. Found one that I liked more than what I saw elsewhere, hence came back. – user1847129 Jul 01 '16 at 19:01