I have an application where we replace an "artproj" file for our application by writing changes to a separate file and then removing the original file and renaming the new one. It generally works well, but sometimes, it gets "stuck" on the delete, going nowhere, with no option to abort the call within Visual Studio when debugging. Looking at Process Explorer, it looks like my application has a handle on the file at that point, but so does explorer.exe. In fact, when I exit the application, explorer.exe still has three handles on it with the same process ID. I can only guess that's why the delete, which uses FileInfo.Delete, doesn't go through.
Here is the deletion procedure:
private static Exception InternalSafeKill(FileInfo TargetFile)
{
try
{
TargetFile.Attributes = FileAttributes.Normal;
TargetFile.Delete();
return null;
}
catch (Exception ex)
{
return ex;
}
}
Is there any way to avoid this sort of conflict? To detect it before the call? To safely escape it in some way when detected?