尝试将目录中的所有文件/目录复制到我创建的新位置。
用户在组合框中选择要使用的“备份驱动器”,然后单击备份桌面按钮,它会在该驱动器上简单地创建一个备份目录并将所有文件复制到该目录中。
备份目录适当地创建在驱动器上 - 但第一个遇到的文件会引发错误。
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);
}
}
我遇到了以下错误:
在“Autos”窗口(VS2010)中,我发现位置已经正确设置为:IOException未处理。
文件名、目录名或卷标语法不正确。
destinationLocation = 相应的目录 (C:\Backups-8-2016\Desktop\)
file = 相应的第一个文件 (C:\Users\myusername\Desktop\myshortcut.url)
我错过了什么?我已经拥有所有复制/粘贴/创建文件的权限,并且存储文件的目录已经被创建——只是移动文件时出现问题。