Ionic.Zip.ZipFile.DeleteFileWithRetry C# (CSharp) Method

DeleteFileWithRetry() private method

Delete file with retry on UnauthorizedAccessException.

When calling File.Delete() on a file that has been "recently" created, the call sometimes fails with UnauthorizedAccessException. This method simply retries the Delete 3 times with a sleep between tries.

private DeleteFileWithRetry ( string filename ) : void
filename string the name of the file to be deleted
return void
        private void DeleteFileWithRetry(string filename)
        {
            bool done = false;
            int nRetries = 3;
            for (int i=0; i < nRetries && !done; i++)
            {
                try
                {
                    File.Delete(filename);
                    done = true;
                }
                catch (System.UnauthorizedAccessException)
                {
                    Console.WriteLine("************************************************** Retry delete.");
                    System.Threading.Thread.Sleep(200+i*200);
                }
            }
        }