Posted By

myke on 01/07/11


Tagged

delete files old


Versions (?)

Delete Files


 / Published in: C#
 

URL: http://digitalcolony.com/2007/02/delete-files-using-c/

From Digital Colony, nice little function that will delete files older than given time frame. Example is 3 minutes. Commented out is a sample of files older than 1 day.

  1. protected void CleanImageFolder()
  2. {
  3. string imgFolder = Server.MapPath("~/lab/maskemail/img/");
  4. string[] imgList = Directory.GetFiles(imgFolder, "*.jpg");
  5. foreach (string img in imgList)
  6. {
  7. FileInfo imgInfo = new FileInfo(img);
  8. if (imgInfo.LastWriteTime < DateTime.Now.AddMinutes(-3))
  9. //if (imgInfo.LastWriteTime < DateTime.Now.AddDays(-1))
  10. {
  11. imgInfo.Delete();
  12. }
  13. }
  14. }

Report this snippet  

You need to login to post a comment.