复制后删除文件会杀死应用程序



在复制文件夹后使用FileInfo.Delete()Directory.Delete(path, true)时,我发现了一个非常奇怪的错误。

问题:

  • 递归复制包含多个文件和几个子文件夹的文件夹
  • 尝试递归删除原始文件夹
  • 正好在文件夹中的第三个文件上,应用程序将在不抛出Exception的情况下崩溃
  • 它并不总是同一个文件,因为前两个文件通常会被删除,第三个文件会失败。(如果我再试一次,第一个将是最后一个失败的,并将被成功删除(

复制:

WPF应用程序中,具有以下方法的按钮会导致崩溃(稍后我将尝试在控制台应用程序中重现(:

private void Tester_Click(object sender, RoutedEventArgs e)
{
string copyFolderToThisPath = @"C:UsersxxxDesktopxxxxxxxx_xxx - Copia";
string copyAndDeleteThisFolder = @"C:UsersxxxDesktopxxxxProvNova pasta (3)";
//copy the directory recursively: works
CopyOrDeleteDirectory(new DirectoryInfo(copyAndDeleteThisFolder), copyFolderToThisPath);
//delete the directory recursively: fails at the third file of the main folder - always
CopyOrDeleteDirectory(new DirectoryInfo(copyAndDeleteThisFolder), null);
}

递归复制或删除:
(我知道Directory.Delete(path, true)存在,但它也失败了,我创建了递归删除来检查发生了什么(

/// <summary>
/// Copies or deletes a directory recursively - Use copyToPath == null to delete
/// </summary>
/// <param name="copyToPath">Destination to copy. If null, will delete the directory</param>
/// <returns>true if all operations with this directory were successful</returns>
private bool CopyOrDeleteDirectory(DirectoryInfo directory, string copyToPath)
{
bool isCopy = !string.IsNullOrEmpty(copyToPath); //defines if the method will copy or delete
FileInfo[] files = null;
DirectoryInfo[] dirs = null;
//try get files and subdirectories
try
{
files = directory.GetFiles();
dirs = directory.GetDirectories();
if (isCopy)
Directory.CreateDirectory(copyToPath);
}
catch (Exception e)
{
MessageBox.Show("Could not enumerate folder " + directory.Name);
return false;
}

bool success = true;
//copy or delete files
foreach (FileInfo file in files)
{
try
{
if (isCopy)
file.CopyTo(System.IO.Path.Combine(copyToPath, file.Name));
else
file.Delete(); //exactly at the third iteration it will crash the application
}
catch (Exception e) //the exception will NEVER be thrown!! Catch block is not reached
{
MessageBox.Show("Failed to operate file " + file.Name);
success = false;
}
}

//copy or delete subdirectories
foreach (DirectoryInfo d in dirs)
{
try
{
string subPath = isCopy ? System.IO.Path.Combine(copyToPath, d.Name) : null;
bool subDirSuccess = CopyOrDeleteDirectory(d, subPath);
if (!subDirSuccess)
success = false;
}
catch (StackOverflowException ex)
{
MessageBox.Show("StackOverflow in recursive function");
success = false;
}
}
if (success && (!isCopy))
{
try
{
directory.Delete();
}
catch (Exception e)
{
MessageBox.Show("Failed to delete directory " + directory.Name);
success = false;
}
}
return success;
}

重要细节:

  • 这是一个";错误再现";密码原始应用程序更大,在复制和删除之间有与用户的交互。这个";复制并删除";不是应用程序的正常流程,但用户可以通过调用应用程序的两个不同命令来实现
  • 在用户单击"复制"和单击"删除"之间,应用程序会有一个完全响应的时间间隔。复制操作在用户可以执行其他操作之前完成
  • 如果只调用delete命令,则不会出现问题,如果先有副本,则会出现问题
  • 我尝试释放资源,但FileInfoDirectoryInfo似乎不可能。不管怎样,我试着尽快将所有地方设置为空,但没有成功
  • 没有抛出Exception,应用程序完全崩溃
  • 所有文件都是可访问的,没有一个是只读的
  • Windows 10
  • 带C的WPF应用程序#
  • Visual Studio 2017调试(问题发生在步进或不步进中(

这可能会有所帮助:

删除临时文件时,C#应用程序无异常退出

我也有同样的问题,总是在第三个文件上。我禁用趋势科技的那一刻,一切都正常了。

相关内容

  • 没有找到相关文章

最新更新