. NET implementation of simple file Incremental Backup program _ Practical Skills

Source: Internet
Author: User

. NET provides many convenient methods, including finding files in processing files, copying files, and so on, today is a simple program to implement incremental backup files.

The first thing you need is to select the backup source file path SourcePath and the backup destination file path DestinationPath, and then stopwatch the time to count the copies. (Note: You need to add a using System.Diagnostics namespace to use stopwatch to add a using System.IO namespace to read and write to a file.)

<summary>///Incremental Backup function method///</summary>///<param name= "SourcePath" > Backup source file path </param>///<p Aram Name= "DestinationPath" > Backup destination file path </param> public void CopyDirectory (string sourcepath, String
  DestinationPath) {Stopwatch watch = new stopwatch (); Watch.   Start ();
  Start calculation time//Check whether the target directory ends with the directory split character if not, add if (Destinationpath[destinationpath.length-1]!= Path.directoryseparatorchar)
  {DestinationPath + = Path.directoryseparatorchar; //Determine if the target directory exists if it does not exist, create a new if (!)
  Directory.Exists (DestinationPath)) {directory.createdirectory (DestinationPath);
  //Get a list of files from the source directory, which is an array containing files and directory paths string[] filelist = directory.getfilesystementries (SourcePath);
     Traverse all files and directories foreach (String sourcefilename in filelist) {string filename = Path.getfilename (sourcefilename); First determine if the file exists in the destination folder if (file.exists (destinationpath + filename)) {FileInfo oldfile = new FileInfo (so
       Urcefilename); FileInfo newFile = new FileiNFO (destinationpath + filename);     if (oldfile.lastwritetime = = Newfile.lastwritetime) {continue; Out of this loop} else {//first as directory processing if present this directory is recursively copy the file under this directory if (directory.exists Sourcefilenam
        e)) {copydirectory (sourcefilename, destinationpath + filename);          }//otherwise direct Copy file else {file.copy (sourcefilename, DestinationPath + filename, true); }} watch.  Stop (); Time to stop MessageBox.Show ("Backup completion is time-consuming" +watch.) Elapsed+ ""); Show the time that is consumed}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.