Click here to Skip to main content
15,889,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have got pdf-file on my hard disk from which I created a copy on an usb stick.
Then I tried to compare the file properties of both versions by using the following code:

C#
DirectoryInfo hardDisk = new DirectoryInfo(hardDisk);
FileInfo[] hardDiskFiles = hardDisk.GetFiles();

DirectoryInfo usb = new DirectoryInfo(usbStick);
FileInfo[] usbFiles = usb.GetFiles();


What I have tried:

The debugger tells me (by inspecting hardDiskFiles / usbFiles) that LastWriteTimeUtc
of the hard disk file is
03.06.2017 08:50:56
while the same property for the usb file shows
03.06.2017 08:50:58
So there is a time difference of two seconds for a date nearly two years in the past. How can that be explained?
Posted
Updated 4-Apr-19 4:28am
v3

The most likely cause is that your HDD is formatted with NTFS, whereas your USB stick is formatted with FAT.

The MSKB article[^] doesn't seem to exist any more, but according to this StackOverflow answer[^]:
Quote:
File time stamps on FAT drives are rounded to the nearest two seconds (even number) when the file is written to the drive. The file time stamps on NTFS drives are rounded to the nearest 100 nanoseconds when the file is written to the drive. Consequently, file time stamps on FAT drives always end with an even number of seconds, while file time stamps on NTFS drives can end with either even or odd number of seconds.

When files are copied from NTFS drives to FAT drives, some file time stamp rounding has to occur; the file time stamp is rounded up to the next even second.
 
Share this answer
 
Comments
Maciej Los 4-Apr-19 12:51pm    
5ed!
Depends on the copy utility.

Dates can be stored in "ticks".

How were the dates copied. Was it binary to "date", then date to binary, and then ...

What were / are the targets and source, their formats and precision?

Rounding from milliseconds to seconds; up or down? Truncate?
 
Share this answer
 
Comments
Member 13566383 3-Apr-19 15:50pm    
I copied the file (manually) with windows explorer (copy and paste), i.e. there was no software of my own involved.
Only after copying the file I started my own software in order to check for differences of properties in both files.
Not really a solution, but maybe a kind of explanation:
I do see exactly the same behavior if I copy my File with File.Copy in my code.
Reading the time in the source file with File.GetLastWriteTimeUtc shows the above mentioned date and time of 03.06.2017 08:50:56 which corresponds to 636320766565495201 internal ticks.
The target file's time stamp is 03.06.2017 08:50:56 which corresponds to 636320766580000000 internal ticks.
Obviously the number of ticks is being rounded when writing them into the target file.
I tried to explicitly copy the source time into the target time with
C#
File.SetLastWriteTimeUtc("<targetPath>", File.GetLastWriteTimeUtc("sourcePath"));

To no avail: The number of ticks gets rounded in the target object.
Overwriting the number of ticks is not possible because the corresponding property is readOnly.
Now I more or less understand what's going on. What I not understand is why this is done in this way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900