title: Delete files older than a date
body: <p>I am currently working on a c# program where I check the creation time of a file and delete it if the file is older than 2 days. I have the following code snippet that should be achieving this. </p>
  
  <pre><code>DateTime creationTime = file.CreationTime.Date;
  if (creationTime &lt; DateTime.Now.AddDays(-logAge) &amp;&amp; file.Name != currentLog)
  {
      File.Delete(string.Format("{0}/{1}", directory, file));
  }
  </code></pre>
  
  <p>While my program is running it is constantly creating new files and a separate thread checks that the files are no older than say 2 days. If I have my PC's date set to the 24th April the files are created and kept as expected, if I then change the PC's date to the 25th April I would expect the files to remain as they are not older than 2 days, however, this is not the case as they are being deleted.</p>
  
  <p>Log age is set to so I wouldn't have expected files to be deleted until after I had changed the date to be the 26th April. </p>
  
  <p>What am I doing wrong, I've looked at many examples including another question on Stackoverflow <a href="https://stackoverflow.com/questions/2222348/how-to-delete-files-older-than-3-months-old-in-a-directory-using-net">Delete files older than 3 months old in a directory using .NET</a> but its not doing what I would expect it to. </p>