File and FileInfo)
[C#][System.IO] About copying folders and the difference between (Directory and DirectoryInfo, File and FileInfo)
[C#][System.IO] About copying folders and the difference between (Directory and DirectoryInfo, File and FileInfo) The problem this time is that I want to copy a folder, but after searching around, I found that only File has Copy or FileInfo’s CopyTo, and there is no copy operation method for Directory. The method of copying a folder in C# is to first create a destination folder (destinationFolder) and then copy the files in the (soursefolder) to the destination folder in sequence. C# does not provide an encapsulated method to copy the files in the folder. Copy the entire file and its subfolders. The following is the CopyFold code: 1 using System.IO; 2 3 class Program 4 { 5 static span> void Main() 6 { 7 string span> sourceFolder = @””; 8 string span> destinationFolder = @””; 9 10 CopyFolder(sourceFolder, destinationFolder); 11 } 12 13 static void CopyFolder(string sourceFolder, string destinationFolder) 14 { 15 DirectoryInfo dir = new DirectoryInfo(sourceFolder); 16 17 Directory.CreateDirectory(destinationFolder); 18 19 FileInfo[] files = dir. GetFiles(); 20 21 foreach (FileInfo file in files) 22 { 23 string span> destinationFile = Path.Combine(destinationFolder, file.Name); 24 file.CopyTo(destinationFile, false); 25 } 26 27 DirectoryInfo[] subDirs = dir. GetDirectories(); 28 29 foreach (DirectoryInfo subdir in…