-1

I need to search on all folders and subfolders to find image files. My problem is that I can not search on a network dir and with this code, it only find onde folder and not all folders and subfolders. Any help? Thank you. What I have:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    List<string> filesList = new List<string>();
    // Create the new DataTable to be used
    tableWithPhotos = new DataTable();
    tableWithPhotos.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
    tableWithPhotos.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
    var diretorios = new List<string>() {@"C:\Users\myfolder\pictures"};
    var extensoes = new List<string>() { "*.jpg", "*.bmp", "*.png", "*.tiff", "*.gif" };
    foreach (string entryExtensions in extensoes)
    {
        foreach (string entryDirectory in diretorios)
        {
        filesList.AddRange(Directory.GetFiles(entryDirectory, entryExtensions, SearchOption.TopDirectoryOnly));
        }
    }
  • 4
    Because you've specified the search option `TopDirectoryOnly`. Make it `AllDirectories`. – Sach Aug 28 '18 at 17:01
  • 1
    In one of your questions, you did have all ready the answer for this, https://stackoverflow.com/questions/51984123/progressbar-on-search-button/51984790?noredirect=1#comment91075460_51984790 and I did put on comment for you for this specifically – Camadas Aug 29 '18 at 16:47

2 Answers2

1

Your immediate problem is you're specifying TopDirectoryOnly for SearchOptions. It should be AllDirectories.

filesList.AddRange(Directory.GetFiles(entryDirectory, entryExtensions, SearchOption.AllDirectories));

To add to the answer, I think you can simplify the process. Creating a list of strings with one directory doesn't make sense. I'm talking about this line:

var diretorios = new List<string>() {@"C:\Users\myfolder\pictures"};

Instead, do something like this:

var topDir = @"C:\Users\myfolder\pictures";
var extensoes = new List<string>() { "*.jpg", "*.bmp", "*.png", "*.tiff", "*.gif" };
foreach (string ext in extensoes)
{
    var files = Directory.EnumerateFiles(topDir, ext, SearchOption.AllDirectories);
    // Add to list.
}

I encourage the use of EnumerateFiles() instead of GetFiles() as it's faster especially if you have a large number if files. See this answer.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • Ok. Good help. But I need to add all files to datatable: `// And now here we will add all the files that it has found into the DataTable foreach (string entryFiles in filesList) { DataRow row = tableWithPhotos.NewRow(); row[0] = Path.GetFileName(entryFiles); row[1] = entryFiles; tableWithPhotos.Rows.Add(row); }` – Paulo Ferreira Aug 29 '18 at 09:06
  • I think this can do what I want: `filesList.AddRange(Directory.EnumerateFiles(allDir, ext, SearchOption.AllDirectories));` – Paulo Ferreira Aug 29 '18 at 10:18
0

This is what I need to do:

filesList.AddRange(Directory.EnumerateFiles(allDir, ext, SearchOption.AllDirectories));