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
Else
{
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.
}
}