Delphi overwrite file and wrong modified date time

元气小坏坏 提交于 2019-12-11 15:55:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!