gregg
gregg

Reputation:

Are windows file creation timestamps reliable?

I have a program that uses save files. It needs to load the newest save file, but fall back on the next newest if that one is unavailable or corrupted. Can I use the windows file creation timestamp to tell the order of when they were created, or is this unreliable? I am asking because the "changed" timestamps seem unreliable. I can embed the creation time/date in the name if I have to, but it would be easier to use the file system dates if possible.

Upvotes: 4

Views: 3697

Answers (7)

lewis
lewis

Reputation: 11

try usining this when you want to rename the file

Path.Combine(ArchivedPath, currentDate + " " + fileInfo.Name))

Upvotes: 0

mzzzzb
mzzzzb

Reputation: 1452

I faced some issues with created time of a file after deletion and recreation under same name.

Something similar to this comment in GetFileInfoEx docs

Problem getting correct Creation Time after file was recreated

I tried to use GetFileAttributesEx and then get ftCreationTime field of the resulting WIN32_FILE_ATTRIBUTE_DATA structure. It works just fine at first, but after I delete file and recreate again, it keeps giving me the original already incorrect value until I restart the process again. The same problem happens for FindFirstFile API, as well. I use Window 2003.

this is said to be related to something called tunnelling

Upvotes: 1

Kim Reece
Kim Reece

Reputation: 1280

What if the system time is changed for some reason? It seems handy, but perhaps some other version number counting up would be better.

Added: A similar question, but with databases, here.

Upvotes: 1

MisterE
MisterE

Reputation:

Windows timestamps are in UTC. So if your timezone changes (ie. when daylight savings starts or ends) the timestamp will move forward/back an hour. Apart from that, and the accuracy of about 2 seconds, there is no reason to think that the timestamps are invalid, and its certainly ok to use them. But I think its bad practice, when you can simply put the timestamp in the name, or in the file itself even.

Upvotes: 1

Larry Osterman
Larry Osterman

Reputation: 16142

Of course if the user of the machine is an administrator, they can set the current time to whatever they want it to be, and the system will happily timestamp files with that time.

So it all depends on what you're trying to do with the information.

Upvotes: 2

user8032
user8032

Reputation: 1221

What do you mean by "reliable"? When you create a file, it gets a timestamp, and that works. Now, the resolution of that timestamp is not necessarily high -- on FAT16 it was 2 seconds, I think. On FAT32 and NTFS it probably is 1 second. So if you are saving your files at a rate of less then one per second, you should be good there. Keep in mind, that user can change the timestamp value arbitrarily. If you are concerned about that, you'll have to embed the timestamp into the file itself (although in my opinion that would be ovekill)

Upvotes: 2

Kent Fredric
Kent Fredric

Reputation: 57374

If you have a directory full of arbitrary and randomly named files and 'time' is the only factor, it may be more pointful to establish a filename that matches the timestamp to eliminate need for using tools to view it.

2008_12_31_24_60_60_1000  

Would be my recommendation for a flatfile system.

Sometimes if you have a lot of files, you may want to group them, ie:

2008/
2008/12/
2008/12/31
2008/12/31/00-12/
2008/12/31/13-24/24_60_60_1000 

or something larger

2008/
2008/12_31/

etc etc etc.

( Moreover, if you're not embedding the time, what is your other distinguishing characteritics, you cant have a null file name, and creating monotonically increasing sequences is way harder ? need info )

Upvotes: 2

Related Questions