using System.IO; using System.Linq;
foreach (var fi in new DirectoryInfo(@"x:\whatever").GetFiles().OrderByDescending(x => x.LastWriteTime).Skip(5))
fi.Delete();
Change the directory name, argument in Skip (), and LastWriteTime to define "oldest."
All the above files, first order them younger, skip the first 5 and delete the rest.
source
share