0

can you help me please on How can we make a automatically backup copy for text file to another location every time the content of file will changed . by using c# ?

i try to make a copy for txt file that used by system program eavery time the content will changed and save every changed on txt file to another location and save it as backup .

noura
  • 3
  • 1
  • There is nothing built-in to *automatically* make a copy. However you can use [File.Copy](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy) in your own code and keep as many backup copies as you like – Hans Kesting Nov 23 '22 at 07:27
  • To detect changes made to the file, you can use FileSystemWatcher. It has its quirks but for not too outlandish tasks it's fine. _But_ maybe also consider using a source code management system instead of copying around. Like Git or Mercurial or whatever you like and can find libs for. – Fildor Nov 23 '22 at 07:37
  • 1
    ^^ [FileSystemWatcher](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-7.0) – Fildor Nov 23 '22 at 07:58
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 23 '22 at 09:50

1 Answers1

0

I hope this can help you as a starting point. And you may want to read about this bug in FileSystemWatcher class

using System;
using System.IO;

namespace Tools
{
    public class AutoBackupFSW
    {
        private string _fileDestination;
        private DateTime _lastBackUp;

        public delegate void BackUpDone(string filePath);
        public event BackUpDone OnBackUpDone;


        public AutoBackupFSW(string directory, string fileDestination)
        {
            _fileDestination          = fileDestination;
            _lastBackUp               = DateTime.Now;
            FileSystemWatcher fsw     = new FileSystemWatcher(directory, "*.txt");
            fsw.EnableRaisingEvents   = true;
            fsw.IncludeSubdirectories = false;
            fsw.NotifyFilter          = NotifyFilters.LastWrite;


            fsw.Changed += Fsw_Changed;
        }

        private void Fsw_Changed(object sender, FileSystemEventArgs e)
        {
            using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                File.WriteAllBytes(_fileDestination, buffer);
            }
            

            if ((DateTime.Now - _lastBackUp).TotalSeconds > 1 && e.ChangeType == WatcherChangeTypes.Changed)
                OnBackUpDone?.Invoke(e.FullPath);

            _lastBackUp = DateTime.Now;
        }
    }
}

and usage:

AutoBackupFSW fs = new AutoBackupFSW(@"F:\test", @"F:\fws\mydoc_backup.txt");
fs.OnBackUpDone += BackUpEngine_OnBackUpDone;
private void BackUpEngine_OnBackUpDone(string filePath)
{
    BeginInvoke(new Action(() =>
    {
       richTextBox1.Text += $"[BACK_UP_LOG]: Back up done at {DateTime.Now.ToShortTimeString()}\r\n";
    }));
}