A way to copy an entire folder (including subfolders) (original)

Source: Internet
Author: User
Original
Need to reference namespaces:
Using System.IO;

<summary>
Copy folders (including subfolders) to the specified folder, both the source folder and the destination folder need an absolute path. Format: CopyFolder (source folder, target folder);
</summary>
<param name= "Strfrompath" ></param>
<param name= "Strtopath" ></param>

//--------------------------------------------------
Author: Kgdiwss qq:305725744
//---------------------------------------------------

public static void CopyFolder (String strfrompath,string strtopath)
{
If the source folder does not exist, create
if (! Directory.Exists (Strfrompath))
{
Directory.CreateDirectory (Strfrompath);
}

Get the name of the folder you want to copy
String strfoldername = Strfrompath.substring (Strfrompath.lastindexof ("\") + 1,strfrompath.length- Strfrompath.lastindexof ("\")-1);

Create a source folder in the destination folder if there are no source folders in the destination folder
if (! Directory.Exists (Strtopath + "\" + strfoldername))
{
Directory.CreateDirectory (Strtopath + "\" + strfoldername);
}
Create an array to save the file name under the source folder
string[] Strfiles = Directory.GetFiles (Strfrompath);

Cycle Copy Files
for (int i = 0;i < strfiles.length;i++)
{
Gets the file name of the copy, takes only the filename, and the address is truncated.
String strFileName = Strfiles[i]. Substring (Strfiles[i]. LastIndexOf ("\") + 1,strfiles[i]. Length-strfiles[i]. LastIndexOf ("\")-1);
Start copying file, true to overwrite file with same name
File.Copy (Strfiles[i],strtopath + "\" + strfoldername + "\" + strfilename,true);
}

Create a DirectoryInfo instance
DirectoryInfo dirinfo = new DirectoryInfo (Strfrompath);
To get all the subfolder names under the source folder
directoryinfo[] Zipath = Dirinfo.getdirectories ();
for (int j = 0;j < zipath.length;j++)
{
Get all child folder names
String Strzipath = Strfrompath + "\" + zipath[j]. ToString ();
Take the resulting subfolder as a new source folder and start a new round of copies from scratch
CopyFolder (Strzipath,strtopath + "\" + strfoldername);
}
}




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.