Skip to content

Instantly share code, notes, and snippets.

@mattjcowan
Created April 25, 2016 02:45
Show Gist options
  • Save mattjcowan/7e6070cd607ed35d74671629c1a73d82 to your computer and use it in GitHub Desktop.
Save mattjcowan/7e6070cd607ed35d74671629c1a73d82 to your computer and use it in GitHub Desktop.
public static void DeleteDirectory(string path, bool recurse = false)
{
try
{
if (Directory.Exists(path))
Directory.Delete(path, recurse);
}
catch (IOException)
{
Thread.Sleep(100);
Directory.Delete(path, recurse);
}
catch (UnauthorizedAccessException)
{
Thread.Sleep(100);
try
{
Directory.Delete(path, recurse);
}
catch
{
// Delete all files in the directory
foreach (var file in Directory.GetFiles(path))
{
try
{
File.Delete(file);
}
catch
{
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment