标签:
using System.IO; /// <summary> /// 拷贝文件 /// </summary> /// <param name="sourceDirectoryUrl">源路经</param> /// <param name="copyDirectoryUrl">复制路经</param> private void CopyDirectory(string sourceDirectoryUrl, string copyDirectoryUrl) { string folderName = sourceDirectoryUrl.Substring(sourceDirectoryUrl.LastIndexOf("\\") + 1); string desfolderdir = copyDirectoryUrl + "\\" + folderName; if (copyDirectoryUrl.LastIndexOf("\\") == (copyDirectoryUrl.Length - 1)) { desfolderdir = copyDirectoryUrl + folderName; } string[] filenames = Directory.GetFileSystemEntries(sourceDirectoryUrl); foreach (string file in filenames)// 遍历所有的文件和目录 { if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1); if (!Directory.Exists(currentdir)) { Directory.CreateDirectory(currentdir); } CopyDirectory(file, desfolderdir); } else // 否则直接copy文件 { string srcfileName = file.Substring(file.LastIndexOf("\\") + 1); srcfileName = desfolderdir + "\\" + srcfileName; if (!Directory.Exists(desfolderdir)) { Directory.CreateDirectory(desfolderdir); } File.Copy(file, srcfileName, true); MessageBox.Show("文件同步成功!"); } }//foreach }
标签:
原文地址:http://www.cnblogs.com/lgq880821/p/4632635.html