FileInfo.LastWriteTime can be misleading. Any other approach?

I used FileInfo.LastWriteTime to find out when was the last time a particular file was modified, say, "c: \ temp \ test_file.txt".

It works great when someone really opens the file, makes changes, and then saves - FileInfo.LastWriteTime for this path is updated with the time when the file was changed.

But what happens in the following scenario:

Copy the file with the same name, but with different contents from another place on the disk, and paste it into the folder "c: \ temp" (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 moment when it was redefined, but actually" LastWriteTime " file that we copied, and may be much earlier than the moment when it was redefined.

In case I need to know the moment when the file was overridden, is there another way and don’t offer such solutions as registering in FileChange events - I’m looking for a solution in which I don’t need to constantly support the program as a service - my application may not run all the time, and I need to know when the file was overridden, even if that moment was when my application was closed.

+4
source share
2 answers

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

I suggest that you check both the recent recording time and the file creation time to determine most of the changes.

Otherwise, you can see the file size, and if this is not enough, then you need to create a hash for the contents of the file.

change

If you use the FileInfo object, you need to call the Refresh method to update any last recording time, create time, file, etc., properties.

+1
source

To do this, you need to use FileInfo.CreationTime () , as well as a combination of FileInfo.LastWriteTime () methods to check.

0
source

All Articles