/// <summary> /// Delete all empty folders within directoryPath that are older than deleteFoldersOlderThanDate. Return the /// number of folders deleted. /// </summary> public static int DeleteAllEmptyFoldersOlderThan(string directoryPath, DateTime deleteFoldersOlderThanDate, bool throwAccessExceptions) { int deleteFolderCount = 0; using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", true, FileSystemEnumerator.EReturnTypes.eReturnDirectories)) { foreach (string dirPath in enumerator) { try { DateTime lastWriteTime = Directory.GetLastWriteTime(dirPath); if (lastWriteTime < deleteFoldersOlderThanDate) { if (!FileUtils.DirectoryContainsFiles(dirPath)) { Directory.Delete(dirPath, true); ++deleteFolderCount; } } } catch (Exception) { if (throwAccessExceptions) { throw; } } } } return(deleteFolderCount); }
/// <summary> /// Delete all files within directoryPath that are older than deleteFilesOlderThanDate. Return the /// number of files deleted. /// </summary> public static int DeleteAllFilesOlderThan(string directoryPath, DateTime deleteFilesOlderThanDate, bool includeSubdirectories, bool throwAccessExceptions) { int deleteFileCount = 0; using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", includeSubdirectories, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string filePath in enumerator) { try { DateTime lastWriteTime = File.GetLastWriteTime(filePath); if (lastWriteTime < deleteFilesOlderThanDate) { File.Delete(filePath); ++deleteFileCount; } } catch (Exception) { if (throwAccessExceptions) { throw; } } } } return(deleteFileCount); }
public static void SafeDeleteAllFilesAndFoldersInFolder(string directoryPath) { if (directoryPath == null) { return; } try { using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", false, FileSystemEnumerator.EReturnTypes.eReturnDirectories)) { foreach (string subFolderPath in enumerator) { FileUtils.SafeDeleteDirectory(subFolderPath); } } using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", false, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string filePath in enumerator) { FileUtils.SafeDeleteFile(filePath); } } } catch (Exception) { } }
public static void DeleteAllFilesInFolder(string directoryPath) { using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", false, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string filePath in enumerator) { File.Delete(filePath); } } }
/// <summary> /// Return the names of all files contained directly within the specified folder. /// </summary> /// <param name="folderPath"></param> /// <returns></returns> public static IList <string> GetFileNamesInFolder(string folderPath) { List <string> names = new List <string>(32); using (FileSystemEnumerator enumerator = new FileSystemEnumerator(folderPath, "*", false, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string filePath in enumerator) { names.Add(Path.GetFileName(filePath)); } } return(names); }
/// <summary> /// Return true if the input directory contains any files. /// </summary> public static bool DirectoryContainsFiles(string directoryPath) { bool isEmpty = true; using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", true, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string filePath in enumerator) { isEmpty = false; break; } } return(!isEmpty); }
/// <summary> /// Return true if the input directory is empty. /// </summary> public static bool IsDirectoryEmpty(string directoryPath) { bool isEmpty = true; using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, "*", false, FileSystemEnumerator.EReturnTypes.eReturnAll)) { foreach (string dirPath in enumerator) { isEmpty = false; break; } } return(isEmpty); }
/// <summary> /// Make a unique file name within the directory directoryPath using fileName as a base name for /// the file. /// </summary> public static string GetOldestFile(string directoryPath, string fileTypesToMatch, bool includeSubDirs) { string filePath = null; DateTime fileTime = DateTime.MaxValue; using (FileSystemEnumerator enumerator = new FileSystemEnumerator(directoryPath, fileTypesToMatch, includeSubDirs, FileSystemEnumerator.EReturnTypes.eReturnFiles)) { foreach (string curFilePath in enumerator) { DateTime curWriteTime = File.GetLastWriteTime(curFilePath); if ((filePath == null) || (fileTime > curWriteTime)) { filePath = curFilePath; fileTime = curWriteTime; } } } return(filePath); }