超时文件删除
/// <summary> /// 超时文件删除 /// </summary> /// <param name="FolderAddress">文件夹地址</param> public static void TimeoutFileDelete(string FolderAddress) { var DA = Directory.GetFiles(FolderAddress); foreach (var item in DA) { DateTime dateTime = Directory.GetCreationTime(item); int DATA = ExecDateDiff(dateTime, DateTime.Now); if (DATA > (60 * 60 * 24 * 7)) //保存7天超过删除 { DeleteFile(item); } } } /// <summary> /// 时间差 /// </summary> /// <param name="dateBegin">开始时间</param> /// <param name="dateEnd">结束时间</param> /// <returns>返回单位秒</returns> public static int ExecDateDiff(DateTime dateBegin, DateTime dateEnd) { TimeSpan ts1 = new TimeSpan(dateBegin.Ticks); TimeSpan ts2 = new TimeSpan(dateEnd.Ticks); TimeSpan ts3 = ts1.Subtract(ts2).Duration(); int Seconds = ts3.Seconds; if (ts3.Minutes > 0) { Seconds += ts3.Minutes * 60; } if (ts3.Hours > 0) { Seconds += ts3.Hours * 60 * 60; } if (ts3.Days > 0) { Seconds += ts3.Days * 60 * 60 * 24; } return Seconds; } /// <summary> /// 根据路径删除文件 /// </summary> /// <param name="path"></param> public static void DeleteFile(string path) { FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } }