c # copy files, folders codes

c # does not copy the code directory, copy the directory needs to be achieved by recursively:

Instructions:

1, the c: \ temp \ Copy all the subdirectories and files in the index directory to c: \ temp under \ newindex directory.

bool copy = CopyDirectory("c:\\temp\\index\\", "c:\\temp\\newindex\\", true);

2, the specific code implementation

You need to reference System.IO namespace codes are as follows:

private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting) { bool ret = false; try { SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; if (Directory.Exists(SourcePath)) { if (Directory.Exists(DestinationPath) == false) Directory.CreateDirectory(DestinationPath); foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo flinfo = new FileInfo(fls); flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false) ret = false; } } RET = to true ; } the catch ( Exception EX ) { RET = to false ; } return RET ; } . 3, directly copy the code to your program done above is merely recorded, reproduced sources: HTTPS: //www.cnblogs .com / meetrice / p / 5397289.html          


Guess you like

Origin www.cnblogs.com/kangao/p/10935411.html