Check If File Created Within Last X Hours

c#

Solution

Use:

System.IO.File.GetCreationTime(filename);

To get the creation time of the file, see GetCreationTime for more details and examples.

Then you can do something like:

public bool IsBelowThreshold(string filename, int hours)
{
     var threshold = DateTime.Now.AddHours(-hours);
     return System.IO.File.GetCreationTime(filename) <= threshold;
}

Problem

How can I check if a file has been created in the last x hours? like 23 hours etc. Using C# 3.0. Note: This must also work if I create the file now, then the file will be seconds old not an hour old yet.

Original source