Search for images in all subdirectories

0

I need to search for images in all subdirectories and display them in picturebox , but the code I currently have is only allowed to search in a single folder and not filtered by file types such as *jpg , *png , etc ...

Below is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace _myfotospf
{
    public partial class FormFotos1 : Form
    {
        public FormFotos1()
        {
            InitializeComponent();
        }

        private void FormFotos_Load(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles(@"C:\Users\...\Imagens");
            DataTable table = new DataTable();
            table.Columns.Add("Nome do ficheiro (duplo clique para ver a miniatura)");
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = new FileInfo(files[i]);
                table.Rows.Add(file.Name);
            }
            dataGridView1.DataSource = table;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            FormFotos2 myForm = new FormFotos2();
            string imageName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            Image img;
            img = Image.FromFile(@"C:\Users\...\Imagens\" + imageName);
            myForm.pictureBox1.Image = img;
            myForm.ShowDialog();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
    
asked by anonymous 03.08.2018 / 12:38

2 answers

0

You can return an IEnumerable with the directories of your files. This method I'm going through will fetch the root directory and subdirectories from the path you've entered.

First parameter is the root directory you want to start fetching your files.

Second parameter is the file search pattern, in this case we are telling you to search for all files of all extensions.

Third parameter we are saying that we will look in all subdirectories from our root path.

The Where clause of Linq is to filter the files that have the extensions that we want to find, in your case you want jpg or png . If you want to insert another extension, simply add another OR condition with the desired extension.

IEnumerable<string> arquivos = Directory.EnumerateFiles(@"C:\Imagens", "*.*", SearchOption.AllDirectories).Where(w => w.ToLower().EndsWith(".jpg") || w.ToLower().EndsWith(".png"));
    
03.08.2018 / 14:03
0

I think this solution is quite comprehensive:

// se pretender ter várias pastas
List<string> diretorios = new List<string>()
{
    @"C:\Pasta1",
    @"C:\Pasta2",
    @"C:\Pasta3"
};

// se pretender pesquisar várias extensões
// é necessário ter um ponto antes da extensão
List<string> extensoes = new List<string>()
{
    ".jpg",
    ".bmp",
    ".png",
    ".tiff",
    ".gif"
};

private void FormFotos_Load(object sender, EventArgs e)
{
    DataTable table = new DataTable();

    table.Columns.Add("Nome do ficheiro (duplo clique para ver a miniatura)");

    foreach (string diretorio in diretorios)
    {
        var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
            Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));

        foreach (var ficheiro in ficheiros)
            table.Rows.Add(Path.GetFileName(ficheiro));
    }

    dataGridView1.DataSource = table;
}
    
03.08.2018 / 14:11