I want to get all of the files in a directory in an array (including the files in subfolders)
string[] filePaths = Directory.GetFiles(@"c:\",SearchOption.AllDirectories);
The problem with this is: If an exception is thrown the entire command stops. Is there a better way to do this so that if a folder cannot be accessed it will just skip over it?
You'd probably have to do a bit more typing yourself then, and write a directory walker like this one:
public static string[] FindAllFiles(string rootDir) {
var pathsToSearch = new Queue<string>();
var foundFiles = new List<string>();
pathsToSearch.Enqueue(rootDir);
while (pathsToSearch.Count > 0) {
var dir = pathsToSearch.Dequeue();
try {
var files = Directory.GetFiles(dir);
foreach (var file in Directory.GetFiles(dir)) {
foundFiles.Add(file);
}
foreach (var subDir in Directory.GetDirectories(dir)) {
pathsToSearch.Enqueue(subDir);
}
} catch (Exception /* TODO: catch correct exception */) {
// Swallow. Gulp!
}
}
return foundFiles.ToArray();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With