SearchOption.AllDirectories and ignore access errors?
string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories);
I'm try开发者_如何学编程ing to search through a directory and all sub directory to find some file. I keep running into an error with the current code that the second it sees something it can't get into it breaks
In this application that doesn't matter i would rather it just move on. Is there anyway to bypass this code from dumping out everytime?
Thanks
Crash893
You could do something like this:
List<string> GetFiles(string topDirectory, string filter)
{
List<string> list = new List<string>();
list.AddRange(Directory.GetFiles(topDirectory, filter));
foreach (string directory in Directory.GetDirectories(topDirectory))
{
list.AddRange(GetFiles(directory));
}
return list;
}
and call it with:
List<string> files = GetFiles(tb_dir.Text, tb_filter.Text);
You could convert the list of files into an array, of course.
You would have to add try catch blocks to handle the UnauthorizedAccessException.
精彩评论