0

I have a form with an add button. When clicked, a user selects a file or files from the dialog.

My Goal:

Retrieve the names of all the files that a user selects (from whichever directory their file(s) are in) , copy those files in a specified folder that the user doesn't choose using File.Copy (I hard-code a filepath and filename).

My Issue:

If the user only selects one, this works fine. For example:

string name = System.IO.Path.GetFileName(sfd.FileName);

This grabs the file. Then:

            DialogResult dialogResult = MessageBox.Show("Is this published?", "", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published");
                File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published\\" + name);
            }
            else if (dialogResult == DialogResult.No)
            {
                Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished");
                File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished\\" + name);
            }

I ask the user if the document is published. Based on the answer, it will create a directory and put the file in that directory.

Is it possible to loop through multiple filenames in the openFileDialog and put them all in a folder , rather than just one?

Josh M
  • 173
  • 5
  • 17

1 Answers1

3

Set the Multiselect to true.

myFileDialog.Multiselect = true;

When the user accepts the selection, you can get them with FileNames property. It returns a string[]. Note the difference with FileName which returns string. You can use a for or foreach to get all the results.

foreach (string file in myFileDialog.FileNames)
{
    //do work
}
Zukki
  • 141
  • 5