/// <summary>
/// 删除文件夹strDir中nDays天以前的文件
/// </summary>
/// <param name="dir"></param>
/// <param name="days"></param>
void DeleteOldFiles(string dir, int days)
{
try
{
if (!Directory.Exists(dir) || days < 1) return;
var now = DateTime.Now;
foreach (var f in Directory.GetFileSystemEntries(dir).Where(f => File.Exists(f)))
{
var t = File.GetCreationTime(f);
var elapsedTicks = now.Ticks - t.Ticks;
var elapsedSpan = new TimeSpan(elapsedTicks);
if(elapsedSpan.TotalDays > days) File.Delete(f);
}
}
catch (Exception)
{
// ignored
}
}