Create a gist now

Instantly share code, notes, and snippets.

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