int retries = 5;
while (retries-- >= 0)
{
try
{
retries = -1;
System.IO.File.Delete(@"C:\deleteme.txt");
}
catch (System.IO.IOException ex) // "The specified file is in use."
{
// File is locked? Sleep and try again.
System.Threading.Thread.Sleep(100); // Wait 100ms
if (retries >= 0) continue; // go back to while loop
throw ex; // give up
}
}
This will also work for writing to a file, etc. This should be enclosed in a try/catch block to catch the other exceptions thrown by the System.IO.File.Delete method.
Comments
Post a Comment