Copy the content in the folder (from the source file (which can contain multiple levels of subfolders) to a folder) using a self-written recursive method)

Source: Internet
Author: User

Private void copyfile (string source, string destination)
{
Bool flag = true;
If (! Directory. exists (destination) & flag = true)
{
Directory. createdirectory (destination );
Flag = false;
}
Directoryinfo rootdir = new directoryinfo (source );

// Traverse objects
Fileinfo [] fileinfo = rootdir. getfiles ();
Foreach (fileinfo file in fileinfo)
{
File. copyto (destination + "\" + file. Name, true );
}
Directoryinfo [] childdir = rootdir. getdirectories ();
For (INT I = 0; I <childdir. length; I ++)
{
Try
{
Fileinfo [] fileinfo_child = childdir [I]. getfiles ();
Foreach (fileinfo file_child in fileinfo_child)
{
File_child.copyto (destination + "\" + file_child.name, true );
}
// Recursive Method
Directoryinfo [] childdir_child = childdir [I]. getdirectories ();
If (childdir_child.length> 0)
{
For (Int J = 0; j <childdir_child.length; j ++)
{
Copyfile (source + "\" + childdir [I]. Name + "\" + childdir_child [J]. Name, destination );
}
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
Continue;
}
}
}
--------------------------------------------------------------
Another method:
// Copy and merge images to a folder
Static void copypictofiledirectory ()
{
Console. writeline ("Enter the source folder path :");
String Spath = console. Readline ();
If (! Directory. exists (Spath ))
{
Console. writeline ("the source folder path does not exist! ");
Return;
}

Console. writeline ("Enter the path of the target Folder :");
String dpath = console. Readline ();
If (! Directory. exists (dpath ))
{
Console. writeline ("the target folder path does not exist! ");
Return;
}


Console. writeline ("whether to delete the original file: Press [y]/[N]");
Bool ismove = false;
String str1 = NULL;
Str1 = console. Readline ();
Bool flag = (str1 = NULL )? False: true;
While (FLAG)
{
If ("Y". Equals (str1.trim (). tolower ()))
{
// Yes
Ismove = true;
Break;
}
Else if ("N". Equals (str1.trim (). tolower ()))
{
// No
Ismove = false;
Break;
}
Console. writeline ("whether to delete the original file: Press [y]/[N]");
Str1 = console. Readline ();
Flag = (str1 = NULL )? False: true;
}

Docopyfile (Spath, dpath, ismove );
}

Private Static void docopyfile (string Spath, string dpath, bool ismove)
{
Directoryinfo = new directoryinfo (Spath );
String destionfilename = NULL;
Fileinfo [] fileinfos = directoryinfo. getfiles ();
Foreach (fileinfo F in fileinfos)
{
Destionfilename = dpath. Trim ('\') + "\" + F. Name;
F. copyto (destionfilename, true );
If (ismove)
{
F. Delete ();
}
}
Directoryinfo [] subdirectories = directoryinfo. getdirectories ();
Foreach (directoryinfo D in subdirectories)
{
Docopyfile (D. fullname, dpath, ismove );
}
}

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.