0
   private void CopyAllFilesToButton_Click_1(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();

        foreach (var item in files)
        {
                File.Copy(item, folderBrowserDialog1.SelectedPath);
        }

    }

Basically, i have a number of file paths. I want to copy each one to a specific folder. What i did, i added folderBrowserDialog from the toolbox and put it inside a button event.

It throws that awkward exception when it reaches File.Copy..why is that, and how can i prevent it?

WithFlyingColors
  • 2,650
  • 4
  • 20
  • 25
  • 1
    What is files? Are the paths correct? what is the error all these would be good things. – rerun Jun 05 '11 at 18:24
  • 1
    Could you supply the callstack of this `ExecutionEngineException`? This type of exception is not normally thrown from `File.Copy`. – Jan-Peter Vos Jun 05 '11 at 19:14

2 Answers2

0

You're not specifying the file to copy to, which is where the exception is coming from.

You're doing File.Copy(item,folderBrownserDialog1.SelectedPath);, while you should be doing File.Copy(item,Path.Combine(folderBrownserDialog1.SelectedPath, item));

That, of course, is if the list of item contains only the filenames, not the full current path to the file. If that's the case, you'll need to do something along these lines:

        foreach (var item in files)
        {
            var fileName = new FileInfo(item);
            File.Copy(item, Path.Combine(folderBrownserDialog1.SelectedPath, fileName.Name));
        }
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
0

Here working solution:

private void buttonCopyFiles_Click(object sender, EventArgs e)
{
   OpenFileDialog od = new OpenFileDialog();
   string destDir = @"D:\dest";
   od.Multiselect = true;

   if (od.ShowDialog() == DialogResult.OK)
   {
      foreach (var file in od.FileNames)
      {
         File.Copy(file, Path.Combine(destDir, Path.GetFileName(file)));
      }               
   }
}

Depending on selected files count and selected files size, your app may hang for a while

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69