2

Possible Duplicate:
Ignore folders/files when Directory.GetFiles() is denied access

I'm writing a program that requires finding the files from the specified path, in addition to all subdirectories. Unfortuantely, some folders the program tries to access return an exception because access is denied (eg. certain system files). I figured that putting a try block around it would have the script ignore the folders that give errors and continue, but instead, it turns out that any time an exception comes up, even if it is caught, it prevents ANY files from being saved to the array. Therefore, if were to run this, then display the array filePaths, it will still come up blank. Is there any way to have the open folders/files still copied to the array, while simply skipping the blocked folders, as opposed to skipping the entire operation?

public getFiles(string path)
{
    string[] filePaths = {};

    string path_ = path;

    try
    {
        filePaths = Directory.GetFiles(path_,"*.*",SearchOption.AllDirectories);
    }

    catch{}
}
Community
  • 1
  • 1
Ari
  • 3,489
  • 5
  • 26
  • 47
  • 5
    The answer is here: http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access Basically, you cannot use SearchOption.AllDirectories. You have to walk the directory tree recursively yourself, try to open each directory, and catch the exception when access is denied. – kol Jun 20 '12 at 21:45
  • @kol Ah, perfect! I searched it before, but didn't find this question in particular. Thank you. – Ari Jun 20 '12 at 21:49

0 Answers0