08: copying files and folders; 08: copying folders

Source: Internet
Author: User

08: copying files and folders; 08: copying folders

Private void btnSave_Click (object sender, EventArgs e) // file copy and save method {# region static copy file (write dead) string desPath = @ "c: \ 1 \ 1.chm "; if (File. exists (desPath) {// the target file already Exists if (MessageBox. show ("file already exists, overwrite"), "Ask", MessageBoxButtons. yesNo, MessageBoxIcon. question) = DialogResult. yes) // select Yes to overwrite {// copy the File. copy (@ "c: \ ls \ w3.chm", desPath, true); MessageBox. show ("Overwrite succeeded") ;}} else // the File does not exist {// Start copying File. copy (@ "c: \ Ls \ w3.chm ", desPath, true); MessageBox. show ("Copied successfully");} // The displayed dialog box. The returned value is of the dialogResult type. If it is OK, the user clicks "open, otherwise, to cancel openFileDialog1.InitialDirectory = (@ "c: \ 1"); // select the default file location // fileter in openfilediaglog1.filter is the role of the filter // showdialog () display the dialog box. openFileDialog1.Filter = "executable program | *. exe | TXT text | *. txt | image file | *. jpg | all files | *. * "; // save type if (openFileDialog1.ShowDialog () = DialogResult. OK) // click open {if (saveFileDialog1.ShowDialo G () = DialogResult. OK) // yes indicates that {File is saved. copy (openFileDialog1.FileName, saveFileDialog1.FileName, true); MessageBox. show ("saved") ;}# endregion} // File class is for File operations, including copying, saving, creation time, modification time, and so on. // The Directory function is similar to file # region dynamic private void btnCopyContents_Click (object sender, EventArgs e) {string oldDir, newDir; // The original folder and target folder FolderBrowserDialog sourceFolder = new FolderBrowserDialog (); // The folderbrowserdialog control is dynamically generated and does not need to be dragged to the control sourceFolder. description = "select the folder to copy"; // a simple Description of if (sourceFolder. showDialog () = DialogResult. OK) // click OK {oldDir = sourceFolder. selectedPath; sourceFolder. descriptio N = "select the folder to be copied"; // modify the description text of sourcefolder to make it easier for users to use if (sourceFolder. showDialog () = DialogResult. OK) // if you are sure, execute the code block {newDir = sourceFolder. selectedPath; // obtain all the files in the folder to be copied (note! Does not contain lower-level folders and their files) string [] files = Directory. getFiles (oldDir); // defines an array of characters to receive the File foreach (string filepath in files) to be copied in the source File. // You can also use the for statement {// File. copy (filepath, newDir + "\" + filepath. substring (filepath. lastIndexOf ("\") + 1), true); // split the file to a more concise string nFileName; // define a string type to obtain the file name nFileName = filepath. substring (filepath. lastIndexOf ("\") + 1); // obtain the File name in the folder to be copied. copy (filepath, newDir + "\" + nFileName, true); // finally obtain the folder to be copied and the file name in the folder to be copied} // MessageBox. show ("copied");} // MessageBox. show (sourceFolder. selectedPath) ;}# endregion

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.