/// <summary> /// Reads filenames from a directory and returns an ArrayList. /// </summary> /// <example> /// <code> /// using Mezzocode.Halide3; /// ... /// ArrayList filenames = h3Storage.GetFiles("/pdf", "*.pdf|*.pdfx", Halide3Lite.h3Storage.PathOptions.Path); /// </code> /// </example> /// <param name="path">Web-formatted path to the files.</param> /// <param name="filter">String filename wildcard (*.* is assumed by default). /// Separate multiple filespecs with a bar character (e.g. "*.gif|*.jpg")</param> /// <param name="options">options for returned files inclusion, et al. /// (e.g. (PathOptions.Hidden | PathOptions.System | PathOptions.Path) returns all files, /// including hidden and system files, and the filename includes the full path).</param> /// <returns>ArrayList of filenames.</returns> public static ArrayList GetFiles(string path, string filter, PathOptions options) { ArrayList fileArray = new ArrayList(); FileInfo objFI; string[] files; if (String.IsNullOrEmpty(filter)) filter = "*.*"; string[] filespec = filter.Split('|'); try { for (int L = 0; L < filespec.Length; L++) { files = System.IO.Directory.GetFiles(path, filespec[L]); for (int i = 0; i < files.Length; i++) { objFI = new System.IO.FileInfo(files[i]); if ( (objFI.Attributes.ToString().IndexOf("Hidden") < 0 && objFI.Attributes.ToString().IndexOf("System") < 0) || ((int)(options & PathOptions.Hidden) != 0 && objFI.Attributes.ToString().IndexOf("Hidden") >= 0) || ((int)(options & PathOptions.System) != 0 && objFI.Attributes.ToString().IndexOf("System") >= 0) ) { if (!fileArray.Contains(objFI.Name) && !fileArray.Contains(objFI.FullName)) { fileArray.Add(((int)(options & PathOptions.Path) != 0) ? objFI.FullName : objFI.Name); } } } } } catch { } return fileArray; }
/// <summary> /// Deletes file(s) from a directory, based on age. /// </summary> /// <example> /// <code> /// using Mezzocode.Halide3; /// ... /// int deletedFilesCount = h3Storage.DeleteFiles("/pdf/", "*.tmp|*.chk", Halide3Lite.h3Storage.PathOptions.Path, DateTime.Now); /// </code> /// </example> /// <param name="path">Web-formatted path to the files or a full path and filename to delete a single file (the later ignores any age params).</param> /// <param name="filter">String filename wildcard (*.* is assumed by default). Separate multiple filespecs with a bar character (e.g. "*.gif|*.jpg").</param> /// <param name="options">options for file inclusion (PathOptions.Hidden | PathOptions.System) deletes all files, including hidden and system files.</param> /// <param name="age">DateTime object; files older than this (creation date; within 1 minute) are deleted.</param> /// <returns>Number of files deleted successfully.</returns> public static Int32 DeleteFiles(string path, string filter, PathOptions options, System.DateTime age) { FileInfo objFI; string[] files; int result = 0; if (String.IsNullOrEmpty(filter)) filter = "*.*"; string[] filespec = filter.Split('|'); if (FileExists(path)) { objFI = new System.IO.FileInfo(path); objFI.Delete(); result++; } else { try { for (int L = 0; L < filespec.Length; L++) { files = System.IO.Directory.GetFiles(path, filespec[L]); for (int i = 0; i < files.Length; i++) { objFI = new System.IO.FileInfo(files[i]); if ( (objFI.Attributes.ToString().IndexOf("Hidden") < 0 && objFI.Attributes.ToString().IndexOf("System") < 0) || ((int)(options & PathOptions.Hidden) != 0 && objFI.Attributes.ToString().IndexOf("Hidden") >= 0) || ((int)(options & PathOptions.System) != 0 && objFI.Attributes.ToString().IndexOf("System") >= 0) ) { if (h3Temporal.DateDiff(h3Temporal.DateDiffComparisonType.minutes, objFI.CreationTime, age) > 1) { objFI.Delete(); result++; } } } } } catch { } } return result; }