C # copy all files in one folder to another, and create a txt file in the new folder to record the file overview and file name of copy.

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;

Namespace ConsoleApplication1
Class Program
Static void Main (string [] args)
// Instantiate the test class, which is used to call the copy function in the class
Test a = new test ();
A. copy ("D :\\ create folder (2)", "D :\\ create folder"); // Test
// Write the test class
Public sealed class test
Public test ()
Public void copy (string source, string destination) // source is the path of the original folder, and destination is the path of the target folder to be copied.
// Call the folder copy Function
Test. CopyDirectory (source, destination );
// Write the content in the original folder to "log .txt" in the target folder
StreamWriter writer = new StreamWriter (destination + "\ record .txt ");
// Directory. GetFileSystemEntries (). You can obtain a string array containing subfolders and file names in the specified Directory.
String [] names = Directory. GetFileSystemEntries (source );
// Read the names array and write all the names of the original file
Foreach (string name in names)
Writer. WriteLine (name );
Writer. Close ();
// Write the folder copy Function
Public static void CopyDirectory (String source, String destination)
DirectoryInfo info = new DirectoryInfo (source );
Foreach (FileSystemInfo fsi in info. GetFileSystemInfos ())
// Target path destName = destination folder path + name of the sub-file (or folder) in the original folder
// Path. Combine (string a, string B) is used to merge two strings.
String destName = Path. Combine (destination, fsi. Name );
// If it is a file class, copy the file
If (fsi is System. IO. FileInfo)
File. Copy (fsi. FullName, destName );
// If not, it is a folder. Continue to call the folder copy function, recursively
Directory. CreateDirectory (destName );
CopyDirectory (fsi. FullName, destName );
// CopyDirectory: this function is referenced by someone else. If it is well written, it will be left behind. I just added a record of information to the outside.

Related Article

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.