Openfiledialog Multiselect

Yousef Imran picture Yousef Imran · May 24, 2014 · Viewed 11.7k times · Source

I am using a multiselect file dialog to browse multiple pictures and add them to a datagridview then from there store them in the database.

Something was wrong in term of that I only managed to store the first selected picture (No syntax or runtime errors at all).

Upon inspection I've realized that the file dialog gets the full path of the first image only and uses it for the rest of images.

Sample code:

if (ofd_pic.ShowDialog() == DialogResult.OK)
{
   foreach (String file in ofd_pic.FileNames)
   {
    MessageBox.Show(ofd_pic.FileName);
   }
}

That messagebox will always show the path of the first image only and I wasn't able to get the path of every single selected image.

The properties of the file dialog are:

1.Modifiers: Private. 2. MultiSelect: True. 3. RestoreDirectory: True.

Any help?

Answer

Sriram Sakthivel picture Sriram Sakthivel · May 24, 2014

You're actually looping through all the files, but you never use it. You need to use the loop variable file

foreach (String file in ofd_pic.FileNames)
{
    MessageBox.Show(file);
}

ofd_pic.FileName property should be used only when you set MultiSelect to false, then only it makes sense. I guess FileName returns the first file when you have enabled MultiSelect.