Eline Sami

Eline Sami

  • NA
  • 47
  • 70.4k

Take a backup copy of a speciifc folder automatically

Aug 18 2014 7:09 AM
I implemented a simple windows form app that take a backup copy of a specific folder and put it in a destination folder that is being chosen by the user.
But, How to simply copy the files in the folder automatically once those files have been changed?  Let's say that you have source files/folder that you would like to maintain a backup of, and it changes a bit day by day. How to simply copy all the files in the target backup folder and overwriting any files that had changed since the last copy operation. Can I do that by code? or I need to use a Scheduled Task to run the utility once per day at a time for example?
 
below is my code:
 

private void GetBackup_Click(object sender, EventArgs e)
        {
          
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
          
           
            if(System.IO.Directory.Exists(sourcePath))
            {
            string[] files = System.IO.Directory.GetFiles(sourcePath);
               
                foreach (string s in files)
                {
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath,fileName);
                    System.IO.File.Copy(s,destFile,true);
                }
                MessageBox.Show("Backup process is complete!", "Taking a backup copy", MessageBoxButtons.OK);
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

        }


Answers (4)