TimeSpan?

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!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
D

Daniel Pratt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top