using DSWeb.Common.DB; using log4net; using MailKit.Net.Smtp; using MimeKit; using Quartz; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Web; namespace DSWeb.Job { /// /// 清理临时文件 /// public class ClearTempJob : IJob { private ILog logger = LogManager.GetLogger("ClearTempJob"); public void Execute(IJobExecutionContext context) { var tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempFiles"); if (!Directory.Exists(tempDir)) return; var arrFiles = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories); if (arrFiles.Length > 0) { logger.Debug($"发现临时文件{arrFiles.Length }个,准备删除"); foreach (var file in arrFiles) { FileInfo fi = new FileInfo(file); if (fi.LastAccessTime < DateTime.Now.AddDays(-1)) { try { File.Delete(file); logger.Debug($"成功删除文件:{file}"); } catch { logger.Error($"删除{file}时出错,未能删除文件"); } } } logger.Debug($"清理临时文件完成"); } var arrDirs = Directory.GetDirectories(tempDir); if (arrDirs.Length > 0) { foreach (var dir in arrDirs) { DirectoryInfo di = new DirectoryInfo(dir); if (di.GetFiles().Length == 0) { Directory.Delete(dir); logger.Debug($"成功删除空目录:{dir}"); } } } } } }