我正在尝试复制文件以创建此文件的备份。递归发生两次,但是自从我进行递归以来,就抛出了此错误。
当我为1个文件执行此功能时,此功能有效。
public void Scan(string path)
{
string[] libs = { "CHIP", "SOT", "SMC", "SOP", "SOJ", "QFP", "PLCC", "PLCC", "SINGLE", "SIP", "DIP", "CON", "ZIG", "BRG", "PAD", "FID" };
try
{
foreach (string lib in libs) {
foreach (var filePath in Directory.EnumerateFiles(path, lib +".lib"))
{
//This is for debugging important in order to differ between used and unused libs
//Here i should create a new instance of a file class that way i can find the lib's back easyly
createBackup(filePath, txtBackup.Text);
}
}
foreach (var dir in Directory.EnumerateDirectories(path)) {
Scan(dir);
}
}
catch (UnauthorizedAccessException) {
Console.WriteLine("Error: " + path);
}
}
public void createBackup(string filePath, string backUpLocation)
{
string fileToBackUp = filePath;
string fileName;
int lastBackSlash = filePath.LastIndexOf("\\") + 1;
string caption;
string message;
fileName = fileToBackUp.Substring(lastBackSlash, fileToBackUp.Length - lastBackSlash);
if (System.IO.File.Exists(backUpLocation + "\\" + fileName) && chk_overWriteAllBackups.IsChecked == false)
{
caption = "File already present!";
message = "Would you like to override the previous back-up that is present in this folder?";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//Backup created in the selected folder and old backup is overwriten
//Add the name of the file to the array of backed up items
System.IO.File.Copy(filePath, backUpLocation + "\\" + fileName, true);
readFiles();
}
else
{
caption = "Are you sure?";
message = "No back-up will be created are you sure?";
result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//Continue without backups
readFiles();
}
else
{
caption = "Select diffrent back-up location?";
message = "Would you like to select a new back-up location?";
result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//Return the user so a new back-up location can be selected
return;
}
else
{
//Continue without backups
readFiles();
}
}
}
}
else
{
try
{
Error-> System.IO.File.Copy(filePath, backUpLocation + "\\" + fileName, true);
}
catch (IOException e)
{
caption = "Error";
message = e.ToString();
System.Windows.Forms.MessageBox.Show(message, caption);
}
}
readFiles();
}
完整错误:
System.IO.IOException:De ingang是ongeldig。\ r \ n \ r \ n bij System.IO .__ Error.WinIOError(Int32 errorCode,字符串 可能是FullPath)\ r \ n bij System.IO.File.InternalCopy(String sourceFileName,字符串destFileName,布尔覆盖,布尔 checkHost)\ r \ n bij System.IO.File.Copy(String sourceFileName,String destFileName,布尔值覆盖)\ r \ n bij test.MainWindow.createBackup(字符串filePath,字符串backUpLocation) 在 C:\ Users \ TomekJasinski \ Documents \ Hello_world \ test \ test \ MainWindow.xaml.cs:row 99