-
-
Save markusl/011ebc276bca4c1f4462 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 throwExceptions = false) | |
{ | |
try | |
{ | |
if (Directory.Exists(path)) | |
{ | |
DirectoryInfo directory = new DirectoryInfo(path); | |
foreach (FileInfo file in directory.GetFiles()) | |
{ | |
try | |
{ | |
file.Delete(); | |
} | |
catch (Exception ex) | |
{ | |
if (throwExceptions) | |
{ | |
throw ex; | |
} | |
} | |
} | |
foreach (DirectoryInfo subdirectory in directory.GetDirectories()) | |
{ | |
try | |
{ | |
Directory.Delete(subdirectory.FullName, true); | |
} | |
catch (Exception ex) | |
{ | |
if (throwExceptions) | |
{ | |
throw ex; | |
} | |
} | |
} | |
try | |
{ | |
Directory.Delete(path); | |
} | |
catch (Exception ex) | |
{ | |
if (throwExceptions) | |
{ | |
throw ex; | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
if (throwExceptions) | |
{ | |
throw ex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment