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