Created
January 9, 2020 10:02
-
-
Save SamKr/6d7d441d03fa9f0f74b803ef7a62de14 to your computer and use it in GitHub Desktop.
Recursively delete folder
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
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