简体   繁体   English

执行引擎异常

[英]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.我做了什么,我从工具箱中添加了 folderBrowserDialog 并将其放在按钮事件中。

It throws that awkward exception when it reaches File.Copy..why is that, and how can i prevent it?当它到达 File.Copy 时,它会抛出那个尴尬的异常。为什么会这样,我该如何防止它?

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);你正在做File.Copy(item,folderBrownserDialog1.SelectedPath); , while you should be doing File.Copy(item,Path.Combine(folderBrownserDialog1.SelectedPath, item)); , 而你应该做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.当然,如果item列表只包含文件名,而不是文件的完整当前路径。 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根据所选文件数和所选文件大小,您的应用可能会挂起一段时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM