ExecutionEngineException
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.Cop开发者_StackOverflowy..why is that, and how can i prevent it?
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));
}
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
精彩评论