/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/// <summary> /// Cleans the working folder of files older than 24 hours /// </summary> private void CleanWorkingFolder() { try { // Ensure the folder exists, if not create it if (Directory.Exists(this.WorkingLocation) == false) Directory.CreateDirectory(this.WorkingLocation); // Ensure the folder exists, if not create it if (Directory.Exists(this.WorkingLocation) == false) return; } catch (IOException ex) { log.Log = "Application"; log.Source = "ImageHelper"; log.WriteEntry("Error trying to delete file: " + ex.ToString(), EventLogEntryType.Error); log.Close(); } catch (Exception exGeneral) { log.Log = "Application"; log.Source = "ImageHelper"; log.WriteEntry("Error trying to delete file: " + exGeneral.ToString(), EventLogEntryType.Error); log.Close(); } foreach (var item in Directory.GetFiles(this.WorkingLocation)) { try { // Delete the file only if it's at least a day old if (info.CreationTime < DateTime.Now.AddDays(-1)) File.Delete(item); } catch (IOException ex) { log.Log = "Application"; log.Source = "ImageHelper"; log.WriteEntry("Error trying to delete file: " + ex.ToString(), EventLogEntryType.Error); log.Close(); } catch (Exception exGeneral) { log.Log = "Application"; log.Source = "ImageHelper"; log.WriteEntry("Error trying to delete file: " + exGeneral.ToString(), EventLogEntryType.Error); log.Close(); } } }