asp.net a tutorial for implementing File and folder replication

Source: Internet
Author: User
Tags file copy

Don't say much, please look at the code:

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
{
Copying files
File.Copy (@ "C:\ls\w3.chm", despath,true);
MessageBox.Show ("cover success");
}
}
else//file does not exist
{
Start replication
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");
}
}
#endregion
}
The file class operates on files, including replication, save, creation time, modification time, and so on.
Directory features similar to file
#region Dynamic
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);
Split a bit more concise
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);
}
}
#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.