开发者

FileInfo.LastWriteTime might be misleading. Any other approach?

I used FileInfo.LastWriteTime to know when was the exact last time where a c开发者_如何学编程ertain file, let's say "c:\temp\test_file.txt" was changed.

It works great when someone is actually opens the file, make changes and then save - the FileInfo.LastWriteTime for this path is being updated with the time the file was changed.

But what happens in the following scenario:

Copy a file with the same name but with different content from other location on disc, and paste it in the "c:\temp\" folder (override the existing file) - the file "c:\temp\test_file.txt" was actually changed (overridden by a file with the same name but with different content), but the LastWriteTime of the file "c:\temp\test_file.txt" is not the time on which it was overridden, but it is actually the "LastWriteTime" of the file which we copied, and might be significantly earlier then the moment in which it was overridden.

In case I need to know the moment in which the file was overridden, is there any other way, and please don't suggest solutions like registering on FileChange events - I'm searching for a solution in which I don't have to keep a program running all the time like a service - my application may not be executed all the time, and I need to know when the file was overridden even if this moment was when my application was closed.


In the circumstance that you describe in your question you can use the file creation time as this will change when you copy a file.

I suggest that you will need to check both the last write time and the file creation time to determine most changes.

Otherwise you could look at file size, and if that's not sufficient, then you'll need to generate a hash on the file contents.

edit

If you are using a FileInfo object you need to call the Refresh method to update any of the last write time, create time, file exists, etc, properties.


For this you need to use FileInfo .CreationTime() & as well as FileInfo .LastWriteTime() methods combination for check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜