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 );
}
}