using log4net; using Quartz; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web; namespace MailSend.Common { /// /// 清理临时文件 /// public class ClearTempJob : IJob { private ILog logger = LogManager.GetLogger("ClearTempJob"); Task IJob.Execute(IJobExecutionContext context) { return Task.Run(() => { var tmpDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp"); var clearCount = 0; if (Directory.Exists(tmpDir)) { var files = Directory.GetFiles(tmpDir); foreach (string file in files) { var fi = new FileInfo(file); if (fi.LastAccessTime < DateTime.Now.AddDays(-1)) { try { File.Delete(file); clearCount++; } catch (Exception ex) { logger.Error(ex.Message); logger.Error(ex.StackTrace); } } } } if (clearCount > 0) { logger.Debug($"成功清理临时文件{clearCount}个"); } }); } } }