- ///
- /// Cleans the working folder of files older than 24 hours
- ///
- 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();
- }
- }
- }
Wednesday, 7 July 2010
DELETE FILES OLDER THAN X-AMOUNT OF TIME
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment