Return to Snippet

Revision: 20697
at November 23, 2009 11:28 by victorboba


Initial Code
/// <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)
            {
                EventLog log = new EventLog();
                log.Log = "Application";
                log.Source = "ImageHelper";

                log.WriteEntry("Error trying to delete file: " + ex.ToString(), EventLogEntryType.Error);
                log.Close();
            }
            catch (Exception exGeneral)
            {
                EventLog log = new EventLog();
                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
                {
                    FileInfo info = new FileInfo(item);
                    // 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)
                {
                    EventLog log = new EventLog();
                    log.Log = "Application";
                    log.Source = "ImageHelper";

                    log.WriteEntry("Error trying to delete file: " + ex.ToString(), EventLogEntryType.Error);
                    log.Close();
                }
                catch (Exception exGeneral)
                {
                    EventLog log = new EventLog();
                    log.Log = "Application";
                    log.Source = "ImageHelper";

                    log.WriteEntry("Error trying to delete file: " + exGeneral.ToString(), EventLogEntryType.Error);
                    log.Close();
                }
            }

        }

Initial URL

                                

Initial Description

                                

Initial Title
Delete files older than x-amount of time

Initial Tags
files

Initial Language
C#