public static void DeleteFolder( string directoryPath, bool recreate = false )
{
int tryCount = 0;
for( ; ; )
{
try
{
if( Directory.Exists( directoryPath ) ) Directory.Delete( directoryPath, true );
if( recreate )
{
Directory.CreateDirectory( directoryPath );
File.WriteAllText( Path.Combine( directoryPath, "TestWrite.txt" ), "Test write works." );
File.Delete( Path.Combine( directoryPath, "TestWrite.txt" ) );
}
return;
}
catch( Exception ex )
{
if( ++tryCount == 20 ) throw;
Console.WriteLine( "{1} - While cleaning up directory '{0}'. Retrying.", directoryPath, ex.Message );
System.Threading.Thread.Sleep( 100 );
}
}
}