我正在试图复制一个文件来创建一个备份。这种情况递归地发生了几次,但是自从我进行这个递归之后,这个错误就会被抛出。
当我对一个文件执行此功能时,该函数就起作用了。
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();
}完全错误:
C:\Users\TomekJasinski\Documents\Hello_world\test\test\MainWindow.xaml.cs:row 99中的\r\n bij System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath)\r System.IO.File.InternalCopy(字符串sourceFileName,字符串destFileName,布尔覆盖,布尔checkHost)\\n bij System.IO.File.Copy(String sourceFileName,String destFileName,布尔覆盖)\r\n bij test.MainWindow.createBackup(String filePath,String backUpLocation)
发布于 2022-10-14 11:32:16
这可能与文件名长度有关--只是在visual studio中尝试打开测试日志文件时出现了类似的错误--它无法创建带有272个字符名的文件。
https://stackoverflow.com/questions/56936947
复制相似问题