Skip to content

Instantly share code, notes, and snippets.

@markusl
Created December 11, 2013 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusl/011ebc276bca4c1f4462 to your computer and use it in GitHub Desktop.
Save markusl/011ebc276bca4c1f4462 to your computer and use it in GitHub Desktop.
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