Private voidBtnsave_click (Objectsender, EventArgs e)//file Copy, save method { #regionCopy files statically (write dead)stringDespath =@"C:\1\1.chm"; if(File.exists (Despath)) {//destination file already exists if(MessageBox.Show ("file already exists, overwrite"),"Ask", Messageboxbuttons.yesno, messageboxicon.question)= = Dialogresult.yes)//Select Yes to determine overwrite { //Copying FilesFile.Copy (@"C:\ls\w3.chm", Despath,true); MessageBox.Show ("Overwrite Success"); } } Else //file does not exist { //Start copyingFile.Copy (@"C:\ls\w3.chm", Despath,true); MessageBox.Show ("Replication succeeded"); } //Displays the Open dialog box, the return value is DialogResult type, if OK, the user clicks the open, otherwise cancelsOpenfiledialog1.initialdirectory= (@"c:\1");//default location When selecting a file//The fileter in Openfilediaglog1.filter is the function of the filter//ShowDialog () Displays the dialog box's method.Openfiledialog1.filter ="executable program |*.exe| TXT text |*.txt| picture file |*.jpg| All files |*.*";//types that can be saved if(Openfiledialog1.showdialog () = = DialogResult.OK)//clicked on the open { if(Savefiledialog1.showdialog () = = DialogResult.OK)//Description Point Yes is confirm save{file.copy (Openfiledialog1.filename, Savefiledialog1.filename,true); MessageBox.Show ("Save Complete"); } }#endregion } //The file class operates on files, including copy, save, create time, modify time, and so on. //directory features like file #regionDynamicPrivate voidBtncopycontents_click (Objectsender, EventArgs e) { stringOlddir, Newdir;//the original folder and the destination folder, respectivelyFolderBrowserDialog Sourcefolder =NewFolderBrowserDialog ();//dynamically generated FolderBrowserDialog this control does not need to drag controlssourcefolder.description="Please select a folder to copy";//shows a simple explanation if(Sourcefolder.showdialog () ==dialogresult.ok)//Click OK.{Olddir=Sourcefolder.selectedpath; Sourcefolder.description="Please select a folder to copy to";//modified the Sourcefolder text to make it easy for users to use if(Sourcefolder.showdialog () = = DialogResult.OK)//If you are sure then execute the following code block code{newdir=Sourcefolder.selectedpath; //get all the files in the folder you want to copy now (note!) Does not contain subordinate folders and the files in them) string[] files = Directory.GetFiles (Olddir);//defines a character array to receive files in the source file that need to be copied foreach(stringFilePathinchFiles//You can also use the For statement { //file.copy (filepath,newdir+ "\ \" +filepath. Substring (filepath. LastIndexOf ("\ \") +1), true); //It 's a little bit more succinct. stringNfilename;//define a string type to get the file nameNfilename = filepath. Substring (filepath. LastIndexOf ("\\") +1);//get the file name in the folder you want to copyfile.copy (filepath, Newdir+"\\"+ Nfilename,true);//Finally, the folder you want to copy and the file name in the folder are copied . } //MessageBox.Show ("copy Complete"); } //MessageBox.Show (sourcefolder.selectedpath); } } #endregion
08 Implementing File and Folder replication