C# 复制文件及文件夹
作者:互联网
/// <summary> /// 复制文件夹及文件 /// </summary> /// <param name="sourceFolder">原文件路径</param> /// <param name="destFolder">目标文件路径</param> /// <returns></returns> public static int CopyFolder(string sourceFolder, string destFolder) { try { //如果目标路径不存在,则创建目标路径 if (!System.IO.Directory.Exists(destFolder)) { System.IO.Directory.CreateDirectory(destFolder); } //得到原文件根目录下的所有文件 string[] files = System.IO.Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = System.IO.Path.GetFileName(file); string dest = System.IO.Path.Combine(destFolder, name); System.IO.File.Copy(file, dest);//复制文件 } //得到原文件根目录下的所有文件夹 string[] folders = System.IO.Directory.GetDirectories(sourceFolder); foreach (string folder in folders) { string name = System.IO.Path.GetFileName(folder); string dest = System.IO.Path.Combine(destFolder, name); CopyFolder(folder, dest);//构建目标路径,递归复制文件 } return 1; } catch (Exception e) { return -1; } }
标签:string,C#,复制,System,dest,文件夹,IO,Path,destFolder 来源: https://www.cnblogs.com/zhangzhiping35/p/16559441.html