Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete folder older than 7 days C# / NET

I want to write some code that deletes all DIRECTORIES older than 7 days.

So:

  1. Check directory : D:\this
  2. If folder older than 7 days -> delete it from the system.
like image 449
1244 Avatar asked Apr 26 '12 22:04

1244


2 Answers

You can lookup using the DirectoryInfo util

   DirectoryInfo d = new DirectoryInfo(dir);
   if (d.CreationTime    < DateTime.Now.AddDays(-7))
       d.Delete();
like image 62
Erre Efe Avatar answered Oct 26 '22 13:10

Erre Efe


You can use DirectoryInfo

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

The voice of experience says to include sanity checks in your code that the directories you are deleting are in fact ones that you want to be deleting...

like image 38
Eric J. Avatar answered Oct 26 '22 15:10

Eric J.