删除方法
private void CleanOldLogs(string logFolderPath)
{
try
{
DirectoryInfo directory = new DirectoryInfo(logFolderPath);
FileInfo[] files = directory.GetFiles();
// 计算删除的日期边界
DateTime boundaryDate = DateTime.Now.AddDays(-15);
foreach (FileInfo file in files)
{
// 如果文件的创建日期早于边界日期,则删除文件
if (file.CreationTime < boundaryDate)
{
file.Delete();
}
}
}
catch (Exception ex)
{
}
}
调用
CleanOldLogs("Log");//程序根目录Log文件夹
标签:文件,log,C#,DateTime,file,CleanOldLogs,删除程序,Log From: https://www.cnblogs.com/banfeng/p/17844545.html