Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two files based on datetime?

Tags:

c#

I need to compare two files based on datetime. I need to check whether these two files were created or modified with same datetime. I have used this code to read the datetime of files...

string fileName = txtfile1.Text;
var ftime = File.GetLastWriteTime(fileName).ToString();
string fileName2 = txtfile2.Text;
var ftime2 = File.GetLastWriteTime(fileName2).ToString();

Any suggestions?

like image 789
bala3569 Avatar asked Dec 06 '10 13:12

bala3569


2 Answers

Don't call ToString() on the DateTime values returned by GetLastWriteTime(). Do this instead:

DateTime ftime = File.GetLastWriteTime(fileName);
DateTime ftime2 = File.GetLastWriteTime(fileName2);

Then you can just compare ftime and ftime2:

if (ftime == ftime2)
{
   // Files were created or modified at the same time
}
like image 118
Donut Avatar answered Oct 11 '22 22:10

Donut


Well, how about doing

ftime == ftime2

? No need for the ToString though, better just compare DateTimes as-is.

like image 21
Matti Virkkunen Avatar answered Oct 11 '22 21:10

Matti Virkkunen