Privatevoidbtnsave_click (Objectsender, EventArgs e)/File copy, save method
#region statically copy files (write dead)
Stringdespath =@ "C:\1\1.chm";
if (file.exists (Despath))
Destination file already exists
if (MessageBox.Show ("file exists, overwrite"), "inquire", Messageboxbuttons.yesno, Messageboxicon.question)
= = Dialogresult.yes)//select Yes to determine overwrite
File.Copy (@ "C:\ls\w3.chm", despath,true);
MessageBox.Show ("cover success");
else//file does not exist
File.Copy (@ "C:\ls\w3.chm", despath,true);
MessageBox.Show ("copy successful");
Show open dialog box, return value is DialogResult type, if OK, the user clicks to open, otherwise cancel
openfiledialog1.initialdirectory= (@ "c:\1");//default location when file is selected
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 file |*.* ";//Can be saved type
if (openfiledialog1.showdialog () = = DialogResult.OK)//click on open
if (savefiledialog1.showdialog () = = DialogResult.OK)//Description Point Yes is confirm save
File.Copy (Openfiledialog1.filename, savefiledialog1.filename,true);
MessageBox.Show ("Save Complete");
The file class operates on files, including replication, save, creation time, modification time, and so on.
Directory features similar to file
Privatevoidbtncopycontents_click (Objectsender, EventArgs e)
Stringolddir, newdir;//is the original folder and the destination folder, respectively
FolderBrowserDialog sourcefolder =newfolderbrowserdialog ()//dynamically generated folderbrowserdialog the control does not need to be dragged
sourcefolder.description = "Please select folder to copy";//show a simple description
if (Sourcefolder.showdialog () ==dialogresult.ok)//dot OK
Olddir = Sourcefolder.selectedpath;
sourcefolder.description = "Please select the folder you want to copy";//Modify the Sourcefolder text to make it easier for the user to use
if (sourcefolder.showdialog () = = DialogResult.OK)//If OK then execute the following code block code
Newdir = Sourcefolder.selectedpath;
Gets all the files in the folder you are currently copying (note!) Does not contain subordinate folders and the files in them)
string[] files = directory.getfiles (olddir);//A character array is defined to receive files that need to be copied within the source file
foreach (stringfilepathinfiles)//can also use a for statement
File.Copy (filepath,newdir+ "\" +filepath). Substring (filepath. LastIndexOf ("\") +1), true);
Stringnfilename//defines a string type to get the filename
Nfilename = filepath. Substring (filepath. LastIndexOf ("\") + 1);//Get the file name in the folder you want to copy
File.Copy (filepath, newdir + "\" + nfilename,true); Finally the folder to copy and the file name in the folder and copy
MessageBox.Show ("copy Complete");
MessageBox.Show (Sourcefolder.selectedpath);