My program creates a log file when it starts. The user has the option through settings to "clear the log" which calls a method to delete the log file.
//calls for a YesNo prompt to delete log or not
result = objectMessageBox.ReturnDeleteLogPrompt();
if (result == DialogResult.Yes)
{
//throw prompt
if (File.Exists(objectLog.GetLogLocation()) == true)
{
try
{
//delete the log file
File.Delete(objectLog.GetLogLocation());
//throw balloon tip saying log was cleared
ShowBalloonTip("LogCleared");
}
catch (Exception ee)
{
MessageBox.Show("Error thrown deleting log: " + ee);
System.Windows.Forms.Clipboard.SetText(ee.ToString());
}
}
}
Because I have deleted the log file entirely I need to then recreate it. So I call a method that has this:
try
{
//we create a new log file so it seems that the log has just been cleared
objectLog.CreateLog();
}
catch (Exception ee)
{
MessageBox.Show("Error occured while clearing log:\n" + ee);
}
But when it attempts to recreate the log file it throws an error that says:
"System.IO.IOException: The process cannot access the file '~~' because it is being used by another process."
So it seems that during my call to delete the file it keeps accessing it? Do I need to dispose of something when I call the file.delete
?
I don't know the details, but there are numerous reasons for why a filename isn't immediately available for recreation after deleting an existing file:
Mercurial had this problem on Windows as well. If you executed one command that locked the repository (which was done using temporary files), and then immediately executed another command that either needed to lock, or at least ensure no lock was present, it could fail with the same type of error, the file was in use, even though this was two distinct processes and the first had already exited.
In other words, the timeline was as follows:
Their hack to "fix" this was to simply pick a random filename that wasn't used in the directory, rename the file to that name, and then delete it. This did not solve the problem of the file lingering for a short while, but it did free up the filename and make it available for new files right away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With