public static void DeleteEntireFolder( this IDirectory system, DirectoryPath path, bool disableReadOnly = true, bool deleteFolderItself = true) { if (!system.Exists(path)) { return; } string[] files = system.GetFiles(path); var exceptions = new List <Exception>(); foreach (string f in files) { var fi = system.FileSystem.FileInfo.FromFileName(f); if (disableReadOnly) { if (fi.IsReadOnly) { fi.IsReadOnly = false; } } try { fi.Delete(); } catch (Exception ex) { exceptions.Add(ex); } } foreach (string subDir in system.GetDirectories(path)) { system.DeleteEntireFolder(subDir, disableReadOnly); } if (deleteFolderItself) { if (system.GetFiles(path).Length == 0 && system.GetDirectories(path).Length == 0) { try { system.Delete(path); } catch (Exception ex) { exceptions.Add(ex); } } } if (exceptions.Count == 1) { throw exceptions[0]; } if (exceptions.Count > 1) { throw new AggregateException(exceptions); } }