/// <summary> /// Purges all files older than the specified number of days from the given directory /// and providing user feedback /// </summary> /// <param name="dir">directory to purge</param> /// <param name="days">number of days</param> /// <param name="callback">callback function for providing user feedback</param> public static void PurgeFiles(DirectoryInfo dir, int days, WinMain callback) { FileInfo[] files = dir.GetFiles(); DateTime cuttoff = DateTime.Now.Subtract( new TimeSpan(days, 0, 0, 0) ); if (callback != null) { callback.ClearOutput("Removing all files older than " + cuttoff.ToShortDateString()); } foreach (FileInfo fileInfo in files) { if (fileInfo.LastWriteTime.CompareTo(cuttoff) < 0) { if (callback != null) { callback.Output(fileInfo.FullName + " " + fileInfo.LastWriteTime.ToShortDateString() + " is old. I shall delete thee."); } fileInfo.Delete(); } else { if (callback != null) { callback.Output(fileInfo.FullName + " " + fileInfo.LastWriteTime.ToShortDateString() + " is not old."); } } } }
/// <summary> /// Purges all files older than the specified number of days from the given directory /// and providing user feedback /// </summary> /// <param name="dir">directory to purge</param> /// <param name="days">number of days</param> /// <param name="callback">callback function for providing user feedback</param> public static void PurgeFiles(DirectoryInfo dir, int days, WinMain callback) { FileInfo[] files = dir.GetFiles(); DateTime cuttoff = DateTime.Now.Subtract( new TimeSpan(days, 0, 0, 0) ); if (callback != null) callback.ClearOutput("Removing all files older than " + cuttoff.ToShortDateString()); foreach (FileInfo fileInfo in files) { if (fileInfo.LastWriteTime.CompareTo(cuttoff) < 0) { if (callback != null) callback.Output(fileInfo.FullName + " " + fileInfo.LastWriteTime.ToShortDateString() + " is old. I shall delete thee."); fileInfo.Delete(); } else { if (callback != null) callback.Output(fileInfo.FullName + " " + fileInfo.LastWriteTime.ToShortDateString() + " is not old."); } } }