TimeSpan?

  • Thread starter Thread starter Philip Townsend
  • Start date Start date
P

Philip Townsend

I need to determine if a file's last write time is greater than 15
minutes. Is there a simple way to do this with the TimeSpan object?
Thanks!
 
Philip,

Yes, there is. If you take the result of the static property Now on the
DateTime type, and then subtract the files last write time, you should get a
TimeSpan instance. Then, you can do the following:

// Assume pobjTimeSpan is the span that holds the difference in time.
// Create a TimeSpan representing 15 minutes.
TimeSpan pobj15Minutes = TimeSpan.FromMinutes(15);

// Now compare to see if the timespan is greater.
if (pobjTimeSpan < pobj15Minutes)

Hope this helps.
 
Hi Philip,

Philip Townsend said:
I need to determine if a file's last write time is greater than 15
minutes. Is there a simple way to do this with the TimeSpan object?
Thanks!

In addition to Nicholas' suggestion, you could also use the TotalMinutes
property of the TimeSpan object.
...
DateTime now = DateTime.Now;
DateTime lastWrite = File.GetLastWriteTime(@"C:\Temp\foo.txt");

TimeSpan interval = now - lastWrite;

if (interval.TotalMinutes >= 15)
{
// do some stuff...
}
...

Regards,
Dan
 
Back
Top