Click to See Complete Forum and Search --> : How to delete older files automatically


pmahavadi
June 16th, 2006, 12:56 AM
Hi ,
I want to delete the files from a folder automatically when files count reaches 10 .When i save a 11th file it should delete the 1 st file(i.e . older file) so that the folder should always have only 10 files.
How to do it??
Thanks in advance.
Lata.

sivaprakash.shanmugam
June 16th, 2006, 01:35 AM
Before creating new file every time check how many files you have in the folder if it reaches 11 then take the first one based one LastWritten time and delete then you can rename the files.

ideru
June 16th, 2006, 02:04 AM
well you can get all the files on the folder first to check how many existing file there is


string[] fname = System.IO.Directory.GetFiles(directoryname)


and from there your can get each of the files attribute and check which one is the oldest and delete it using "File.Delete(fileName)"