Question

        string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories);

I'm trying 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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top