. Net-based incremental file backup system implementation and. net Incremental Backup implementation
. Net provides many easy-to-use methods, including searching for files in processing files and copying files. Today, Incremental backup files are implemented through simple programs.
First, you need to select the backup source file path SourcePath and the backup target file path DestinationPath, and then use StopWatch to calculate the copy time. (Note: To use StopWatch, you must add the using System. Diagnostics namespace. You must add the 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 target 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 a directory delimiter. if not, add if (DestinationPath [DestinationPath. length-1]! = Path. DirectorySeparatorChar) {DestinationPath + = Path. DirectorySeparatorChar;} // you can create if (! Directory. exists (DestinationPath) {Directory. createDirectory (DestinationPath);} // obtain the file list of the source Directory, which is an array containing files and Directory paths string [] fileList = Directory. getFileSystemEntries (SourcePath); // traverses all files and directories foreach (string SourceFilename in fileList) {string filename = Path. getFileName (SourceFilename); // first, judge whether the object exists in the target folder if (File. exists (DestinationPath + filename) {FileInfo oldFile = new FileInfo (SourceFilename); FileInfo newFile = new FileInfo (DestinationPath + filename); if (oldFile. lastWriteTime = newFile. lastWriteTime) {continue; // jumps out of this loop }}
Else {// first as a Directory. if this Directory exists, recursively Copy the files under this Directory if (Directory. exists (SourceFilename) {CopyDirectory (SourceFilename, DestinationPath + filename);} // otherwise, Copy the else {File directly. copy (SourceFilename, DestinationPath + filename, true );
}}} Watch. Stop (); // time Stop
MessageBox. Show ("backup completion time" + watch. Elapsed + ""); // display consumed time}