0

this is my code below. I've already created a filter that searches for any all image file extensions but when my code runs the SearchOption.AllDirectories appears to be trying to open a particular path instead of searching all my directories.

Anyone help me on where I've gone wrong here?

string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };
var directory = new DirectoryInfo(lblText.Text);
var files = new List<FileInfo>();

foreach (var filter in filters)
{
    var results = directory.GetFiles(filter, SearchOption.AllDirectories);
    files.AddRange(results);
}

Thanks for any help! :)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Milly
  • 25
  • 1
  • 1
  • 6
  • 2
    Exactly what is `filter` set to? – Matthew Watson Oct 24 '13 at 14:50
  • I've updated my code window. – Milly Oct 24 '13 at 15:07
  • Ahh, so now I have to ask: what specifically is the value of `lblText.Text`? If it's "" then this won't work for you (it will only search the current working directory and its subdirectories) – Matthew Watson Oct 24 '13 at 15:14
  • Thank you. I've solved it with all of your help. I think I was just being an idiot. :) – Milly Oct 24 '13 at 15:21
  • 2
    You should really make an effort to write a full, complete and small question. `lblText.Text` is just uninformative; you could easily use a string literal instead. Maybe avoid using `var` as we don't have intelisense on StackOverflow. – Buh Buh Oct 24 '13 at 15:22
  • @MatthewWatson: the constructor of `DirectoryInfo` doesn't accept an empty string("The path is not of a legal form."). – Tim Schmelter Oct 24 '13 at 15:30

2 Answers2

2

I assume directory is a DirectoryInfo object and you're using this overload of GetFiles. Then a FileInfo[] is returned from the current directory matching the given search pattern and searching all subdirectories.

So the directory-path of the DirectoryInfo is the root directory.

For example:

DirectoryInfo imageDir = new DirectoryInfo(@"c:\Images");
FileInfo[] allJPGImages = imageDir.GetFiles(".jpg",  SearchOption.AllDirectories);

Edit according to your edit.

So the particular path is the Text entered/shown in lblText. Another way to get all files with these extensions:

string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };
List<FileInfo> files = filters
    .SelectMany(filter => directory.EnumerateFiles(filter, System.IO.SearchOption.AllDirectories))
    .ToList();

which does not need to load all files into memory until it starts processing. when you are working with many files and directories, EnumerateFiles can be more efficient.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I am not sure what filter is in your code, but here is a simple example to search a directory.

            string path = "C:\\myFolder1\\myFolder2";
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files;               
            files = dir.GetFiles("*.*", SearchOption.AllDirectories);

Maybe your path is wrong? But the AllDirectories options begins at your specified path.

Zath
  • 547
  • 2
  • 10
  • 25