/// <summary> /// Delete a file. If the deleteEmptyDirectoresRecursively is true all empty directories /// from buttom up are also deleted. /// </summary> /// <param name="path"></param> /// <param name="deleteEmptyDirectoresRecursively"></param> public static void DeleteFile(string path, bool deleteEmptyDirectoresRecursively) { LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting file '{0}'", path)); C1File.Delete(path); if (deleteEmptyDirectoresRecursively) { string directory = Path.GetDirectoryName(path); string[] directories = directory.Split(Path.DirectorySeparatorChar); for (int i = directories.Length; i > 1; --i) { StringBuilder stringBuilder = new StringBuilder(); for (int j = 0; j < i; ++j) { stringBuilder.Append(directories[j]); stringBuilder.Append(Path.DirectorySeparatorChar); } string currentPath = stringBuilder.ToString(); if (C1Directory.GetFiles(currentPath).Length == 0) { LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting directory '{0}'", currentPath)); C1Directory.Delete(currentPath); } else { break; } } } }
/// <exclude /> public static void Delete(string filePath) { if (C1File.Exists(filePath)) { RemoveReadOnly(filePath); C1File.Delete(filePath); } }
/// <summary> /// Deletes all files recursively leaving all sub directories. /// </summary> /// <param name="directoryPath"></param> public static void DeleteFilesRecursively(string directoryPath) { foreach (string file in C1Directory.GetFiles(directoryPath)) { C1File.Delete(file); } foreach (string directory in C1Directory.GetDirectories(directoryPath)) { DeleteFilesRecursively(directory); } }