. 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)!