7

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?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Yousef Imran
  • 1,071
  • 2
  • 10
  • 16
  • How you say that it display only one path ? What happen when you select two file then it display filename with messagebox. What happen when you close that message box. Does other messagebox get popup? – dotnetstep May 24 '14 at 07:30

2 Answers2

12

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.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

You have used wrong parameter.

if (ofd_pic.ShowDialog() == DialogResult.OK)
{
foreach (String file in ofd_pic.FileNames)
 {
 MessageBox.Show(file);
 }
}
dotnetstep
  • 17,065
  • 5
  • 54
  • 72