FolderBrowserDialog nested folders

2k Views Asked by At

I have a folder. The folder has multiple folders in it, each folder has an image in it. Like this:

Main Folder>Subfolder 1>Picture 1

Main Folder>Subfolder 2>Picture 2

etc.

I'm looking to select the main folder using FolderBrowserDialog, and somehow display all the Pictures in the sub folders (Picture 1, Picture 2, etc.) Is this possible with FolderBrowserDialog? If not, how could i go about doing this? Thanks

3

There are 3 best solutions below

1
On
Directory.GetFiles("path", "*.jpeg", SearchOption.AllDirectories);
0
On

Yes it's possible, but the FolderBrowserDialog is just a portion of the solution. It might look something like this:

using (var fbd = new FolderBrowserDialog())
{
    if (fbd.ShowDialog() == DialogResult.OK)
    {
        foreach (var file in Directory.GetFiles(fbd.SelectedPath,
            "*.png", SearchOption.AllDirectories)
        {
            // this catches things like *.png1 or *.pngp
            // not that they'd ever exist; but they may
            if (Path.GetExtension(file).Length > 4) { continue; }

            var pictureBox = new PictureBox();
            pictureBox.Load(file);

            // set its location here

            this.Controls.Add(pictureBox);
        }
    }
}

This code only searches for png files, and it's worth noting that the reason I check the extension is because of the little known caveat on 3-character extension searches:

A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern.

0
On

Using Folder Browser dialog user can only select folders and not files. So may be could have your own control to display the list of images exist in selected folder.

        FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
        DialogResult dlgResult = folderBrowserDlg.ShowDialog();
        if (dlgResult.Equals(DialogResult.OK))
        {
            foreach (string file in System.IO.Directory.GetFiles(folderBrowserDlg.SelectedPath, "*.png")) //.png, bmp, etc.
            {
                Image image = new Bitmap(file);
                imageList1.Images.Add(image);                 
            }
        }

        this.listView1.View = View.LargeIcon;
        this.imageList1.ImageSize = new Size(32, 32);
        this.listView1.LargeImageList = this.imageList1;
        for (int j = 0; j < this.imageList1.Images.Count; j++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = j;
            this.listView1.Items.Add(item);
        }

The Above code will fetch the list of image files present in selected folder and add them into the listview control.

enter image description here