4

I am using this code to search all directories in all drives to search for all .txt files:

public List<string> Search()
{
    var files = new List<string>();
    foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
    {
        files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories));
     }
     return files;
}

but in running I am having this error: enter image description here

How to resolv it?

Thank you.

bbb
  • 263
  • 2
  • 4
  • 16
  • Write error message as a text. People might not see the image(for example, me). – Soner Gönül Jul 12 '13 at 10:45
  • 1
    @wudzik With using try-catch it will not find any files.Because it ignores all other directories when facing with the directory in the above arror. – bbb Jul 12 '13 at 10:48
  • @SonerGönül Access to the path 'C:\$Recycle.Bin\S-1-5-21-2163108302-3039877246-2377335864-500' is denied. – bbb Jul 12 '13 at 10:49
  • http://stackoverflow.com/questions/17570629/how-to-get-path-from-different-files-in-different-directories/17570928#17570928 Check my solution where I had the same issue with accessing the file. – Rudi Jul 12 '13 at 10:52
  • @bbb nope, it'll find in directories which you can access – Kamil Budziewski Jul 12 '13 at 10:52
  • @wudzik - I wouldn't expect that behaviour. I'd expect that maybe some files might be found, but as soon as an exception is thrown, the complete `AddRange` will be aborted, so files in directories "after" the inaccessible one will *not* be found. – Corak Jul 12 '13 at 10:58
  • @wudzik I tried it.It will return nothing. – bbb Jul 12 '13 at 11:25

2 Answers2

7

This is simply a permissions problem. Use a try/catch block. Some of the folders on your disk including RecycleBin folders are not accessible to unprivileged code.

public List<string> Search()
{
    var files = new List<string>();
    foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady))
    {
        try
        {
            files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories));
        }
        catch(Exception e)
        {
            Logger.Log(e.Message); // Log it and move on
        }
    }

    return files;
}

Also note that using Directory.GetFiles with AllDirectories option has an inherent problem that it will fail if ANY of the folders in the entire drive is not accessible, and thus you'll not get any files for that drive in your results. The solution is to do manual recursion. An excellent example of that approach is available in this SO question.

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • I did it,but i saw that if it ignores the whole drive with its directories with the first error.I think I need first to check directory accessibility and then search there but I dont know how to do that. – bbb Jul 12 '13 at 10:52
  • @bbb: See my comment on Slartibartfast's answer. – dotNET Jul 12 '13 at 10:55
  • 1
    @dotNET - Slartibartfast removed his answer and went back to Magrathea to design more Fjords. – Corak Jul 12 '13 at 11:00
  • @Corak: Ouch. You're one highly informed man out there :). Anyway I added correct sample code above. – dotNET Jul 12 '13 at 11:03
  • @DotNEt thanks for the corrections, i was on a phone with poor controls when I was writing that answer, thought it'd be best to delete it, this answer is correct. -Corak Haha you caught me – Vrashabh Irde Jul 15 '13 at 06:40
0

The problem here is that some files you don't have control on it to get access it's files. So you need to use try{ } catch{ }. But i think you can't handle this while processing the whole file system directories at once. SO you need to first get list of all directories then process each directory at once at while you are processing specific directory files you can handle that kind of exception.

Kindly check this:

unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure

Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60
  • I am a little confused with the arguments.First with the string path argument;I want to search all directories not a spesific one.And the with the IList files;What is this exactly?Could you please explain me more? – bbb Jul 12 '13 at 11:17
  • first you get just all directories paths then for each directory path you call method AddFiles() that 'll add the files to a global array. and if you can't read the file then you catch the exception and don't add this file. – ebram khalil Jul 12 '13 at 11:26