0

Hi I am trying to search for all files and folders that start with a given sequence of characters in a specific path. (The path is given by the user and so is the sequence).

To accomplish this I have been using the Directory.GetFiles and Directory.GetDirectories methods. However when I search with no search option defined, it doesn't return all the files / folders that could've been found. So I added the SearchOption.AllDirectiories argument but when it's present, the function doesn't return anything. This is my code:

try
{
    // Search for directories 

    foreach (string d in Directory.GetDirectories(path, $"{param}*", SearchOption.AllDirectories))   
    {
        Console.WriteLine(d);
        found_directories.Add(d);
    }

    Console.WriteLine("");
    // Search for files

    foreach (string f in Directory.GetFiles(path, $"{param}*", SearchOption.AllDirectories))
    {
        Console.WriteLine(f);
        found_files.Add(f);
    }

}
catch (UnauthorizedAccessException)
{
    // Some directories cannot be accessed and hence cause the program to crash 
    // So it's neccesary to catch the error
}

break;

I have already looked at these posts (which didn't help me):

Directory.GetFiles Not returning a file

Directory.GetFiles(path, ".txt", SearchOption.AllDirectories); doesn't deliver a file

Directory.GetFiles - SearchOption.AllDirectories Does not give files in subfolders (vb)

Laurent Gabiot
  • 1,251
  • 9
  • 15
  • Forgot to explain, the break statement at the end is there because all of this code runs in a case of a switch statement. – GetTwoBirdsStonedAtOnce Nov 22 '21 at 12:03
  • Does the `{param}` contains a dot? – Jeroen van Langen Nov 22 '21 at 12:09
  • What is `param` value? When I set it as empty, everything works. – kosist Nov 22 '21 at 12:13
  • @JeroenvanLangen no it does not, it's a sequence that user enters, it can be for example: "abc" and I added the wildcard character at the end so it looks for files that start with that sequence – GetTwoBirdsStonedAtOnce Nov 22 '21 at 12:14
  • @kosist param is the search pattern – GetTwoBirdsStonedAtOnce Nov 22 '21 at 12:15
  • You already have a comment explaining what is happening! `Some directories cannot be accessed and hence cause the program to crash` I suggest you add a `Console.WriteLine()` to the `catch` block to output the exception message. – Matthew Watson Nov 22 '21 at 12:29
  • @Matthew Watson That is not it!! I already have been writing out the error to the console and it caused an infinite loop! I know what the error is, it does not give access to some SYSTEM directories, however I highly doubt that that's the issue when I am searching through my pictures folder... – GetTwoBirdsStonedAtOnce Nov 22 '21 at 12:43
  • May be you had an infinite loop, not because of the comments, but because of your logic? – Vadim Nov 22 '21 at 13:01
  • 1
    `Console.WriteLine()` won't cause an infinite loop, and neither will any of the code you've posted so far, so it would appear that there is a problem elsewhere. – Matthew Watson Nov 22 '21 at 13:13
  • @Matthew Watson None of this is relevant to my actual problem... The Getfiles and Getdirectory functions do not return all the files that can be found, when used with Searchoption.Alldirectories parameter, the functions don't return anything – GetTwoBirdsStonedAtOnce Nov 22 '21 at 13:26
  • Best of luck solving it then! – Matthew Watson Nov 22 '21 at 13:34
  • Note that, once an `UnauthorizedAccessException` occures, the execution will stop. Note that, as Mattew Watson already said, that exception may occur during the execution of `Directory.GetDirectories(...)` which will then return not a single entry and move to the catch block. Add a Console.WriteLine into the catch block and show us your output with a sample folder. – Chrᴉz remembers Monica Nov 22 '21 at 14:12
  • It shows Permission denied and then the path that it's been denied access too – GetTwoBirdsStonedAtOnce Nov 22 '21 at 14:58
  • See, that's the reason. `Directory.GetDirectories()` throws a `UnauthorizedAccessException` -> Catches the exception and you reach your `break` statement without ever adding something to your list `found_directories`. After catching an exception, the flow continues inside the catch block and then after the catch block, not back where the exception was thrown. – Chrᴉz remembers Monica Nov 22 '21 at 15:14
  • So you are saying I should add a "finally" or "else" statement after catch, in order to repeat the search process? – GetTwoBirdsStonedAtOnce Nov 22 '21 at 15:31
  • @ChrᴉzremembersMonica I just want the code to ignore the error and keep on searching – GetTwoBirdsStonedAtOnce Nov 22 '21 at 15:42

1 Answers1

1

So after doing a little bit of research I found the issue.

  • Directory.GetFiles() / Directory.GetDirectories() are inherently flawed. When any exception occurs the function does not continue searching for files / folders. It's because there's no error handling inside these functions. Trying to write the try/catch yourself is non beneficial, since even when you catch an exception, the function will finish without returning anything.

Is there a solution?

No, but there's a workaround.

The workaround

Use this github repo instead: https://github.com/astrohart/FileSystemEnumerable/

It is an improved version of the function, it does all the error handling so you don't need to worry about anything, I tested it myself and it works for me.

The github repo is realated to this stackoverflow question.

For anyone that would maybe have the same problem in the future, I recommend you check out this link.

I hope that by answering my own question, I helped someone in the future. Cheers :)!