执行引擎异常
private void CopyAllFilesToButton_Click_1(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
foreach (var item in files)
{
File.Copy(item, folderBrowserDialog1.SelectedPath);
}
}
基本上,我有很多文件路径。我想将每个文件复制到特定文件夹中。我所做的,我从工具箱中添加了folderBrowserDialog,并将其放入按钮事件中。
当它到达 File.Copy 时,它会抛出那个尴尬的异常。为什么会这样,我该如何防止它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有指定要复制到的文件,这就是异常的来源。
你正在做
File.Copy(item,folderBrownserDialog1.SelectedPath);
,而你应该做File.Copy(item,Path.Combine(folderBrownserDialog1.SelectedPath, item));
当然,这是如果
item
列表仅包含文件名,而不包含文件的完整当前路径。如果是这种情况,您需要按照以下方式执行操作: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 doingFile.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:这里的工作解决方案:
根据所选文件数量和所选文件大小,您的应用程序可能会挂起一段时间
Here working solution:
Depending on selected files count and selected files size, your app may hang for a while