How to copy all the directories and files in a directory

Source: Internet
Author: User
This article describes how to copy all the files in a directory to the target directory.
Here are a few of the classes that we will use in this routine:
1, Directory:exposes static methods for creating, moving, and enumerating through directories and subdirectories.
2. Path:performs operations on String instances that contain file or directory Path information. These are operations are performed in a cross-platform manner.
3, File:provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
All of these two classes are not inheritable and are inherited directly from object and are implemented as sealed, all of which are explained from MSDN.
Here is the implementation code, the details of the code are not described here in detail, see Code Comments:
// ======================================================
Implement a static method to copy all content under the specified folder to the target folder
// ======================================================
public static void Copydir (String srcpath,string aimpath) {
Checks whether the destination directory ends with a directory split character if it is not added
if (Aimpath[aimpath.length-1]!= Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Determine if the target directory exists if it does not exist the new
if (! Directory.Exists (Aimpath)) directory.createdirectory (Aimpath);
Gets a list of files in the source directory that contains an array of files and directory paths
If you point to the file below the copy destination file and not the directory, use the following method
string[] filelist = Directory.GetFiles (Srcpath);
string[] filelist = directory.getfilesystementries (Srcpath);
Iterate through all the files and directories
foreach (string file in FileList) {
First as a directory processing if there is this directory recursively copy the file below the directory
if (directory.exists (file))
Copydir (file,aimpath+path.getfilename (file));
Otherwise direct copy file
Else
File.Copy (file,aimpath+path.getfilename (file), true);
}
}

Hey! This time to say that the function is relatively simple, suitable for beginners, I hope to help beginners! If you need to use this feature in Web engineering, set sufficient permissions for the ASPNET account, but this is dangerous and is not recommended.



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.