views:

53

answers:

2

Hi,

I used FileInfo.LastWriteTime to know when was the exact last time where a certain file, let's say "c:\temp\test_file.txt" was CHANGED.

It works greate 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 (overriden 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 overriden, but it is actually the "LastWriteTime" of the file which we copied, and might be significantly earlier then the moment in which it was overriden.

In case I need to know the moment in which the file was overriden, is there any other way, AND PLEASE DON'T SUGGEST SOLUTIONs like registerring 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 overriden even if this moment was when my applicatoin was closed.

Thanks at Advance!

+1  A: 

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.

Enigmativity
Hi Enigmatifity,It doens't work. The FileInfo.CreationTime does not correspond with the moment of the paste (overriden) operation, but is earlier than that.Any other suggestions?Thanks at Advanced!
Liorp
Hi Enigmatifity, I read your 'edit' and tried to call the FileInfo.Refresh() method before looking at the last write, access and creation time, but it didn't help.
Liorp
Any other suggestions?
Liorp
A: 

Hi Enigmatifity,

It doens't work. The FileInfo.CreationTime does not correspond with the moment of the paste (overriden) operation, but is earlier than that.

Any other suggestions?

Thanks at Advanced!

Liorp
You should use the "add comment" link rather than posting a new answer. I'll edit my original answer to give you the further suggestion.
Enigmativity