C# Windows Forms - Select and Copy Multiple Files w MultiSelect, OpenFileDialog, & FileBrowserDialog

Brian McCarthy picture Brian McCarthy · Mar 16, 2011 · Viewed 18.7k times · Source

I'm developing a WinForms application using C# with an OpenFileDialog and FileBrowserDialog and I'd like to:

  1. Enable selection of multiple xls files.
  2. After selection is made, Display selected xlsx filenames in textbox
  3. Copy the selected files to a separate directory Consolidated

How can I accomplish this?

Answer

Anton Semenov picture Anton Semenov · Mar 16, 2011

Here is sample code:

        OpenFileDialog od = new OpenFileDialog();
        od.Filter = "XLS files|*.xls";
        od.Multiselect = true;
        if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string tempFolder = System.IO.Path.GetTempPath();

            foreach (string fileName in od.FileNames)
            {
                System.IO.File.Copy(fileName, tempFolder + @"\" + System.IO.Path.GetFileName(fileName));
            }
        }