public static bool DeleteDirectory( string path, bool recursive )
{
bool deleted = true;
try
{
/*
// Delete all files and sub-folders?
if (recursive)
{
// Yep... Let's do this
string[] subfolders = Directory.GetDirectories( path );
foreach (string s in subfolders)
{
DeleteDirectory( s, recursive );
}
}
// Get all files of the folder
string[] files = Directory.GetFiles( path );
foreach (string f in files)
{
// Get the attributes of the file
FileAttributes attr = File.GetAttributes( f );
// Is this file marked as 'read-only'?
if (( attr & FileAttributes.ReadOnly ) == FileAttributes.ReadOnly)
{
// Yes... Remove the 'read-only' attribute, then
File.SetAttributes( f, attr ^ FileAttributes.ReadOnly );
}
// Delete the file
File.Delete( f );
}
// When we get here, all the files of the folder were
// already deleted, so we just delete the empty folder
var di = new DirectoryInfo(path);
if ((di.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
di.Attributes &= ~FileAttributes.ReadOnly;
Thread.Sleep( 100 );
*/
//Directory.Delete( path, true );
try { DeleteDir(path); }
catch (Exception)
{
Thread.Sleep(1000); try { DeleteDir(path); }
catch (Exception) { Thread.Sleep(1000); DeleteDir(path); }
}
}
catch (Exception e)
{
LogManager.Error( e, "An error has occurred deleting directory \"{0}\"", path );
deleted = false;
}
return deleted;
}