【发布时间】:2017-01-08 10:00:25
【问题描述】:
尝试将目录中的所有文件/目录复制到我创建的新位置。 用户在组合框中选择要使用的“备份驱动器”,然后当他们单击备份桌面按钮时,它只会在该驱动器上创建一个备份目录并将所有文件复制到该目录中。
在驱动器上适当地创建了备份目录 - 但它遇到的第一个文件会引发错误。
private void backupDesktopButton_Click(object sender, EventArgs e)
{
//set the destionationLocation to the selectedDrive
string selectedDrive = backupDriveCombo.SelectedItem.ToString();
string destinationLocation = selectedDrive+"Backups-" + DateTime.Now.Month.ToString()+"-"+DateTime.Now.Year.ToString()+"\\Desktop\\";
if (!Directory.Exists(destinationLocation))
{
Directory.CreateDirectory(destinationLocation);
}
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string[] fileList = Directory.GetFiles(desktopFolder);
foreach (string file in fileList)
{
//move the file
File.Copy(file, destinationLocation);
}
}
我得到错误:
IOException 未处理。
文件名、目录名或卷标语法不正确。
在“自动”窗口 (VS2010) 中,我看到位置设置正确:
destinationLocation = 适当的目录 (C:\Backups-8-2016\Desktop\)
file = 相应的第一个文件 (C:\Users\myusername\Desktop\myshortcut.url)
我错过了什么?我有权复制/粘贴/创建内容并创建存储它的目录 - 只是移动文件时出现问题。
【问题讨论】:
标签: c#