Problem to set the right Time of a File

  • Thread starter Thread starter Tim B
  • Start date Start date
T

Tim B

Hi

i have a big proglem.

I want to set the last write time of a file.
i try it over the FileInfo object and the
File.SetLastWriteTime method
but i have allways a difference of one more second !

i am going mad!

it is important for me because i want to synchronise files.

i compare them by last write time.

Thx for comments and help!

Tim
 
Hi Tim,

SetLastWriteTime should use exactly what time you give it.
I tried using both SetLastWriteTime and LastWriteTime, both set the correct time.
However, using <dir> or Explorer they both report not one second, but a full hour more.
Retrieving the lastwritetime from that file gave the correct time.

It seems windows uses a different calculation when displaying last write time, but you should still be able to get the correct time programmatically.
 
but why it doesn't work

here my code

....

FileInfo fi = new FileInfo(offlinefile); // offlinefile is a string

DateTime myNewDate = file.lastmodified; //file is a own object ,
lastmodified is a DateTime Object


Console.WriteLine("date myNewDate file " + myNewDate.ToString() );

Console.WriteLine("date server file " + file.lastmodified.ToString() );

Console.WriteLine("date lokal file " + fi.LastWriteTime.ToString() );


File.SetLastWriteTime(offlinefile,myNewDate);

fi = new FileInfo(offlinefile);

Console.WriteLine("date lokal file " + fi.LastWriteTime.ToString() );

output:

date myNewDate file 23.09.2004 11:16:41

date server file 23.09.2004 11:16:41

date lokal file 23.09.2004 13:11:58

date lokal file 23.09.2004 11:16:42



....
 
Well, I would take a closer look at the file object and how it obtains the timestamp.
If you don't have access, try using the Win32 API and get the lastmodified time.

If I understand this page correctly

http://www.xxcopy.com/xxcopy15.htm

there seems to be a 2 second granularity on FAT systems.
It may be that this causes the differing times.
 
Back
Top