Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openfiledialog Multiselect

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?

like image 204
Yousef Imran Avatar asked May 24 '14 07:05

Yousef Imran


2 Answers

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.

like image 148
Sriram Sakthivel Avatar answered Sep 21 '22 19:09

Sriram Sakthivel


You have used wrong parameter.

if (ofd_pic.ShowDialog() == DialogResult.OK)
{
foreach (String file in ofd_pic.FileNames)
 {
 MessageBox.Show(file);
 }
}
like image 29
dotnetstep Avatar answered Sep 19 '22 19:09

dotnetstep