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)