How to copy all directories and files in a directory

xiaoxiao2021-03-06  61

This article describes how to copy all files in a directory to the target directory. Here are a few of the routines in the class we want to use: 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 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 in these two categories are. If it is not inherited, it is directly inherited from Object, which is implemented as Sealed, and the above explanation is from MSDN.

Here is the code implemented, the details in the code are not described in detail, please see the code comment: / / ============================= =========================== // Implement a static method to set all the contents below the specified folder COPY below // == ============================================================================================================================================================================================================= == Public Static Void CopyDir (String Srcpath, String Aimpath) {// Checks if the target directory ends with directory split characters If not, add IF (AIMPATH [AIMPATH.LENGTH-1]! = path.directoryseparatorchar) AIMPATH = PATH .DirectoryseParatorChar; // Determine if the target directory exists if there is no new IF (! Directory.exists (AIMPATH)) Directory.createdIRectory (AIMPATH); // is gave a list of files, which is included in the contents and directory paths. An array // If you point to the file under the COPY target file without including a directory, please use the following method // String [] filelist = Directory.getFiles (srcpath); string [] filelist = Directory.getFileSystemTementries (srcpath); / / Traverse all files and directory foreach (string file in file in filelist) {// first as directory processing If there is this directory, recursive COPY this directory below the file IF (Directory.exists (file) CopyDir (File, Aimpath Path .GetFileName (file)); / / otherwise directly copy file else file.copy (file, aimpath path.getFileName (file), true);}} 嘿嘿! This time, it is relatively simple, suitable for beginners, I hope to help beginners! If you need to use this feature in a web project, then set it to the ASPNET account enough permissions, but doing this is very dangerous, it is not recommended.

If you have any questions or suggestions you can send an email to: wu_jian830@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-118189.html

New Post(0)