How to compare two files based on datetime?

I need to compare two datetime based files. I need to check if these two files were created or modified with the same date-time. I used this code to read datetime files ...

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

Any suggestions?

+6
source share
5 answers

Do not 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 simply compare ftime and ftime2 :

 if (ftime == ftime2) { // Files were created or modified at the same time } 
+15
source

Well how about

 ftime == ftime2 

? There is no need for ToString, but it is better to compare DateTimes as is.

+2
source

GetLastWriteTime () returns a DateTime object, so you can do this:

 if (File.GetLastWriteTime(filename).CompareTo(File.GetLastWriteTime(filename2)) == 0) 

to check if they have the same timestamp.

For more information on comparing DateTime objects, see.

+1
source

The accuracy of the time of the last change stored in the file system differs in different file systems (VFAT, FAT, NTFS). Therefore, for this, it is best to use the epsillon environment and either let the user choose a reasonable value, or choose a value based on the file systems involved.

https://superuser.com/questions/937380/get-creation-time-of-file-in-milliseconds

 double epsillon = 2.0 DateTime lastUpdateA = File.GetLastWriteTime(filename); DateTime lastUpdateB = File.GetLastWriteTime(filename2); if (Math.Abs(Math.Round((lastUpdateA - lastUpdateB).TotalSeconds)) > epsillon) different = true; 
0
source
 bool isMatched=false; int ia = string.Compare(ftime.ToString(), ftime2.ToString()); if (string.Compare(ftime.ToString(), ftime.ToString()) == 0) isMatched = true; else isMatched = false; 
-2
source

All Articles