Code
/// <Summary>
/// Copy all content under the specified folder to the target folder
/// </Summary>
/// <Param name = "srcpath"> Original path </Param>
/// <Param name = "aimpath"> Target folder </Param>
Public Void Copydir ( String Srcpath, String Aimpath)
{
Try
{
// Check whether the target directory ends with a directory delimiter. If not, add it.
If (Aimpath [aimpath. Length - 1 ] ! = Path. directoryseparatorchar)
{
Aimpath + = Path. directoryseparatorchar;
}
// Determine whether the target directory exists. If it does not exist, create it.
If (Directory. exists (aimpath ))
{
This . Deletefolder (aimpath. substring ( 0 , Aimpath. Length - 1 ));
}
Directory. createdirectory (aimpath );
// If you direct to the file under the copy target file without including the directory, use the following method:
String [] Filelist = Directory. getfilesystementries (srcpath );
// Traverse all files and directories
Foreach ( String File In Filelist)
{
// First, process the file as a directory. If this directory exists, recursively copy the file under this directory.
If (Directory. exists (File ))
Copydir (file, aimpath + Path. getfilename (File ));
// Otherwise, copy the file directly.
Else
File. Copy (file, aimpath + Path. getfilename (file ), True );
}
}
Catch (Exception ee)
{
Throw New Exception (EE. tostring ());
}
}