Copy the entire folder (including subfolders ).

Source: Internet
Author: User
Namespace to be referenced:
Using system. IO;

/// <Summary>
/// Copy a folder (including subfolders) to the specified folder. Both the source folder and the target folder must be in the 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 );
}

// Obtain the name of the folder to be copied
String strfoldername = strfrompath. substring (strfrompath. lastindexof ("\") + 1, strfrompath. Length-strfrompath. lastindexof ("\")-1 );

// If no source folder exists in the target folder, create the source folder in the target folder.
If (! Directory. exists (strtopath + "\" + strfoldername ))
{
Directory. createdirectory (strtopath + "\" + strfoldername );
}
// Create an array to save the file name in the source folder
String [] strfiles = directory. getfiles (strfrompath );

// Copy objects cyclically
For (INT I = 0; I <strfiles. length; I ++)
{
// Get the copied file name. Only the file name is taken and the address is truncated.
String strfilename = strfiles [I]. substring (strfiles [I]. lastindexof ("\") + 1, strfiles [I]. length-strfiles [I]. lastindexof ("\")-1 );
// Start copying an object. True indicates overwriting an object of the same name.
File. Copy (strfiles [I], strtopath + "\" + strfoldername + "\" + strfilename, true );
}

// Create a directoryinfo instance
Directoryinfo dirinfo = new directoryinfo (strfrompath );
// Obtain the names of all subfolders in the source folder
Directoryinfo [] zipath = dirinfo. getdirectories ();
For (Int J = 0; j <zipath. length; j ++)
{
// Obtain the names of all subfolders
String strzipath = strfrompath + "\" + zipath [J]. tostring ();
// Use the obtained subfolder as the new source folder and start a new round of copying from the beginning
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.