This article mainly shared the implementation of file and folder copy of the sample code, with a certain reference value, following the small series to see it together
If you don't say much, look at the code:
private void Btnsave_click (object sender, EventArgs e)//File copy, save method {#region Static copy file (write dead) string despath = @ "C: \1\1.chm "; if (file.exists (Despath)) {//target file already exists if (MessageBox.Show ("File already exists, overwrite"), "ask", Messageboxbuttons.yesno , messageboxicon.question) = = dialogresult.yes)//select Yes to determine overwrite {//Copy file File.Copy (@ "c:\ls\w 3.chm ", Despath, True); MessageBox.Show ("Overwrite success"); }} else//file does not exist {//start copying File.Copy (@ "C:\ls\w3.chm", Despath, True); MessageBox.Show ("copy succeeded"); }//Show open dialog box, return value is DialogResult type, if OK, then the user click on Open, otherwise cancel openfiledialog1.initialdirectory= (@ "c:\1"); The default location when you select a file the Fileter in//openfilediaglog1.filter is the function of the filter//showdialog () The method that displays the dialog box. Openfiledialog1.filter = "executable program |*.exe| TXT text |*.txt| picture file |*.jpg| all Files |*.* ";//Can save type if (openfiledialog1.showdialog () = = DialogResult.OK)//Click on open {if (Savefiledialog1.showdialog () = = DialogResult.OK)//Description point YES is also confirmation save {file.copy (Openfiledialog1.filename, Savefiledialog1.filename, true); MessageBox.Show ("Save Complete"); }} #endregion} The//file class is a file operation, including copying, saving, creation time, modification time, and so on. The directory functions like file #region dynamic private void Btncopycontents_click (object sender, EventArgs e) {string Olddi R, Newdir; The original folder and the destination folder FolderBrowserDialog Sourcefolder = new FolderBrowserDialog ();//Dynamically generated FolderBrowserDialog this control does not need to drag the control sourcefolder.description = "Please select the folder to copy";//shows a simple description if (Sourcefolder.showdialog () ==dialogresult.ok)//dot OK { Olddir = Sourcefolder.selectedpath; sourcefolder.description = "Select the folder you want to copy to";//Modify the Sourcefolder text to make it easier for users to use if (sourcefolder.showdialog () = = Dialogres Ult. OK)//If OK 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 a subordinate folder and the files in it) string[] files = directory.getfiles (olddir);//defines a character array to receive the file that needs to be copied in the source file foreach (String fil Epath in fiLes)//You can also use the For statement {//file.copy (filepath,newdir+ "\ \" +filepath. Substring (filepath. LastIndexOf ("\ \") +1), true); Split a bit, more concise string nfilename; Define a string type to get the file name Nfilename = filepath. Substring (filepath. LastIndexOf ("\ \") + 1); Gets the file name in the folder to be copied File.Copy (filepath, newdir + "\ \" + Nfilename, True); Finally, the folder to be copied and the file name in the folder and copy}//messagebox.show ("Copy Complete"); }//messagebox.show (Sourcefolder.selectedpath); }} #endregion