I am trying to delete all pdf files older than 1 hour, I am using this code
System.IO.DirectoryInfo directory = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["PDFFilesPath"]);
var files = directory.GetFiles("*.pdf").Where(f => DateTime.Now.Subtract(f.CreationTime).TotalMinutes >= int.Parse(System.Configuration.ConfigurationManager.AppSettings["MinutesOld"]));
foreach (var file in files)
{
file.Delete();
}
The code is working fine and as expected.
But I am wondering if there is any way to not use foreach or any other smaller approach to achieve this
Thanks in advance!