What is the best way to detect a change in a file and make a backup of it when it changes?

Cynolycus 255 Reputation points
2023-11-15T11:01:14.0733333+00:00

As the title says I want ideas on the best way to detect a change in a file and create a backup of it when it changes.

I have an external program that saves or updates a file at irregular intervals.

I want my app to detect any change in this file and create a backup of it.

Several years ago I created an app that did this using FileSystemWatcher and it saved the backups with the time included in the name.

The problem that I had with it was that the external program seemed to update the file in pieces, so every time it saved the file it was actually saving it multiple times and only the last time it saved was it complete.

If I remember correctly I built in a timer so my app would make a backup as soon as the file changed then it would wait a short time and make another backup if there was another change detected during that time, and if no change was detected it didn't create another backup.

This meant that the number of backups was reduced to two or three backups each time the file was saved, but only the last backup was complete.

I have created another app that does much more but backing up this file is part of it.

Should I do it the same way as before but maybe have it keep track of the backups it makes each time and delete all but the last backup, or is there a better way to do this?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,713 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.