. NET implementation of simple file Incremental backup program

Source: Internet
Author: User
. NET provides many easy-to-use methods, including finding files in processing files, copying files, and so on, today is the implementation of 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 spent on the statistical copy. (Note: Using the stopwatch requires adding a using System.Diagnostics namespace, adding a using System.IO namespace to read and write files.)

<summary>///Incremental Backup Function method///</summary>///<param name= "SourcePath" > Backup source file path </param>///< param 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 destination directory ends with a directory split character if not add if (destinationpath[destinationpath.length-1]! = Path.directoryseparatorchar) {  DestinationPath + = Path.directoryseparatorchar; }//To determine if the target directory exists if it does not exist then new if (!  Directory.Exists (DestinationPath)) {directory.createdirectory (DestinationPath);  }//Get the file list of the source directory, which is an array containing the file and directory path 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 (sourcefi       Lename); FileInfo newFile = new FileInfo (DestinationPath + Filename);     if (oldfile.lastwritetime = = Newfile.lastwritetime) {continue;        Jump out of this loop}} else {///first as directory processing if this directory is present, recursively copy the file under the directory if (directory.exists (sourceFileName))        {copydirectory (sourcefilename, destinationpath + filename);          }//otherwise direct Copy file else {file.copy (sourcefilename, DestinationPath + filename, true); }}} watch.  Stop (); Time Stops MessageBox.Show ("Backup complete time-consuming" +watch. Elapsed+ ""); Show the time Consumed}



The above is. NET implement Simple file incremental Backup program content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.