问题
I'd like to get a file last modified time in Delphi.
Normally something like FileAge()
would do the trick, only the problem is: if I overwrite *File A* with File B using CopyFile
, File A's modified date is not updated with current overwrite time as it should(?)
I get that: CopyFile
also copy file attributes, but I really need to get the modified date that also works when a file is overwritten.
Is there such function? My whole application relies on modification time to decide whether or not I should proceed with files!
EDIT Just to clarify: I'm only monitoring the files. It's not my application who's modifying them.
回答1:
The documentation for CopyFile says:
File attributes for the existing file are copied to the new file.
Which means that you cannot use base your program on the last modified attribute of the file, or indeed any attribute of the file. Indeed there are all sorts of ways for the last modified attribute of the file to change. It can in fact go backwards in time.
Instead I suggest that you use ReadDirectoryChangesW to keep track of modifications. That will allow you to receive notifications whenever a file is modified. You can write your program in an event based manner based on the ReadDirectoryChangesW
API.
If you can't use ReadDirectoryChangesW
and the file attributes, then you'll have to base your decisions on the contents of the file.
来源:https://stackoverflow.com/questions/14487764/delphi-overwrite-file-and-wrong-modified-date-time