/// <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
}
}
版权声明:本文为BombZhang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。