Created
April 25, 2016 02:45
-
-
Save mattjcowan/7e6070cd607ed35d74671629c1a73d82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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