Skip to content

Instantly share code, notes, and snippets.

@SamKr
Created January 9, 2020 10:02
Show Gist options
  • Save SamKr/6d7d441d03fa9f0f74b803ef7a62de14 to your computer and use it in GitHub Desktop.
Save SamKr/6d7d441d03fa9f0f74b803ef7a62de14 to your computer and use it in GitHub Desktop.
Recursively delete folder
internal static void DeleteFolderRecursively(string rootMap)
{
var created = DateTime.MinValue;
try
{
created = Directory.GetCreationTime(rootMap);
foreach (var map in Directory.GetDirectories(rootMap))
{
DeleteFolderRecursively(map);
}
foreach (var file in Directory.GetFiles(rootMap))
{
var fileCreated = DateTime.MinValue;
try
{
fileCreated = File.GetCreationTime(file);
var pPath = Path.Combine(rootMap, file);
File.SetAttributes(pPath, FileAttributes.Normal);
File.Delete(file);
}
catch (Exception ex)
{
Log.LogException(new Exception("Kan bestand niet verwijderen!", new Exception($"Map: {file}, created: {fileCreated:yyyy-MM-dd HH:mm:ss}")), ex);
}
}
var directory = new DirectoryInfo(rootMap) {Attributes = FileAttributes.Normal};
Directory.Delete(directory.FullName);
}
catch (Exception ex)
{
Log.LogException(new Exception("Kan map niet verwijderen!", new Exception($"Map: {rootMap}, created: {created:yyyy-MM-dd HH:mm:ss}")), ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment