Reputation: 11010
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?
Upvotes: 7
Views: 18035
Reputation: 1061
The precision of the last modification time being stored in a file system varies between different file systems (VFAT, FAT, NTFS). So, its best to use an epsillon environment for this and either let the user choose a sensible value or choose a value based on the involved file systems.
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;
Upvotes: 2
Reputation: 20
bool isMatched=false;
int ia = string.Compare(ftime.ToString(), ftime2.ToString());
if (string.Compare(ftime.ToString(), ftime.ToString()) == 0)
isMatched = true;
else
isMatched = false;
Upvotes: -3
Reputation: 100718
GetLastWriteTime() returns a DateTime object, so you can do this:
if (File.GetLastWriteTime(filename).CompareTo(File.GetLastWriteTime(filename2)) == 0)
to test if they have the same timestamp.
See this for more tips on comparing DateTime objects.
Upvotes: 1
Reputation: 112855
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
}
Upvotes: 18
Reputation: 65156
Well, how about doing
ftime == ftime2
? No need for the ToString though, better just compare DateTimes as-is.
Upvotes: 2