gpt4 book ai didi

c# - C#中随机抛出的IO异常

转载 作者:行者123 更新时间:2023-11-30 17:21:32 25 4
gpt4 key购买 nike

我一直致力于为我工作的公司归档旧客户数据的程序。该程序将数据从工作服务器复制到本地计算机,创建所有数据的 .zip 文件,然后将其复制到存档服务器。

完成所有操作后,它会删除原始文件和本地副本。时不时地,程序会出错,因为它无法从我的计算机上删除本地副本。它不会在压缩的每个不同文件夹中出错。我会在执行 300 次或 5 次后出错。它会抛出以下 3 个错误之一,“目录不为空”、“文件正在被另一个进程使用”或“拒绝访问文件”。我已经尝试将文件属性设置为正常,使用强制垃圾收集,并手动结束 winzip 进程。

我真的不明白为什么它只是偶尔这样做。我是我电脑的管理员,它应该能够删除文件。我认为必须有其他东西在使用它,但除了 Visual Studio 中的程序外,我的机器上应该没有其他东西在使用它。谢谢。

下面是不删除文件的清理方法和压缩文件的方法。

[MethodImplAttribute(MethodImplOptions.NoInlining)]
static void CleanUp(SqlConnection Connection, string jNumber, DirectoryInfo dir, bool error, string prefix)
{
if (!error | (!error & emptyFolder))
{
try
{
SqlCommand updateJob = new SqlCommand(string.Format("update job set archived = 1 where job = {0}", jNumber), sdiConnection);
updateJob.ExecuteNonQuery();
}
catch
{
WriteToLog("SQL Error: " + jNumber, "There was an error changing the archive bit to 1 after the job has been archived");
}

try
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch
{
WriteToLog("Error cleaning up after processing job", "There was an error garbage collecting.");
}


try
{
//path of the temporary folder created by the program
string tempDir = Path.Combine(Path.Combine(System.Environment.CurrentDirectory, "Temp"), jNumber);
//path of the destination folder
string destDir = Path.Combine(dir.ToString(), jNumber);
//SetFileAttributes(tempDir);


try
{
File.Delete(tempDir + ".zip");
}
catch (System.IO.IOException)
{
File.Delete(tempDir + ".zip");
}
try
{
Directory.Delete(destDir, true);
}
catch (System.IO.IOException)
{
Directory.Delete(destDir, true);
}
try
{
Directory.Delete(tempDir, true);
}
catch (System.IO.IOException)
{
Directory.Delete(tempDir, true);
}

}
catch
{

WriteToLog("File Error: " + jNumber, "There was an error removing files and/or folders after the job has been archived. Please check the source server and destination server to make sure everything copied correctly. The archive bit for this job was set.");
Directory.Delete(Path.Combine(System.Environment.CurrentDirectory, "Temp"), true);
Directory.CreateDirectory(Path.Combine(System.Environment.CurrentDirectory, "Temp"));
}
}


static bool ZipJobFolder(string jNumber, string jPath)
{
try
{
string CommandStr = @"L:\ZipFiles\winzip32.exe";
string parameters = "-min -a -r \"" + jNumber + "\" \"" + jPath + "\"";

ProcessStartInfo starter = new ProcessStartInfo(CommandStr, parameters);
starter.CreateNoWindow = true;
starter.RedirectStandardOutput = false;
starter.UseShellExecute = false;

Process process = new Process();
process.StartInfo = starter;
Console.WriteLine("Creating .zip file");
process.Start();
process.WaitForExit();

Process[] processes;
string procName = "winzip32.exe";
processes = Process.GetProcessesByName(procName);
foreach (Process proc in processes)
{
Console.WriteLine("Closing WinZip Process...");
proc.Kill();
}

}
catch
{
WriteToLog(jNumber, "There was error zipping the files of this job");
return false;
}
return true;
}

最佳答案

我在使用 Windows 资源管理器删除包含大量文件和子文件夹的大文件夹时注意到了这种行为。但稍等片刻后再次删除,它似乎工作正常。

因此,我一直认为这是操作系统的不稳定行为。

虽然这不是解决方案,但您可以尝试让您的应用程序休眠一小段时间,然后再尝试删除这些文件,看看错误是否仍然出现。

如果错误消失,则可能与某些时间问题有关。不过,我自己也想知道问题的根源。

评论者指向反病毒程序。这是有道理的,如果这是真的,那么您需要编写一些代码来检查文件是否在尝试删除之前被锁定。如果它被锁定然后休眠一会儿,然后再次检查直到它不再被锁定,您可以继续删除它。

您只需要注意不要陷入无限竞争状态即可。

编辑:有一个关于 How to best wait for a filelock to release 的相关问题检查它的想法。

编辑2:这是另一种可能的解决方案 Wait until file is unlocked in .Net

关于c# - C#中随机抛出的IO异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3338473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com