C# 拷贝目录
public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name="oldpath">源目录</param> /// <param name="newpath">新目录</param> public static void CopyDirectory(string oldpath, string newpath) { if (string.IsNullOrWhiteSpace(newpath)) return; var folderName = oldpath.Substring(oldpath.LastIndexOf("\\", StringComparison.Ordinal) + 1); var desfolderdir = newpath + "\\" + folderName; if (newpath.LastIndexOf("\\", StringComparison.Ordinal) == (newpath.Length - 1)) { desfolderdir = newpath + folderName; } var filenames = Directory.GetFileSystemEntries(oldpath); foreach (string file in filenames) { if (Directory.Exists(file)) { var currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1); if (!Directory.Exists(currentdir)) { Directory.CreateDirectory(currentdir); } CopyDirectory(file, desfolderdir); } else { var srcfileName = file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1); srcfileName = desfolderdir + "\\" + srcfileName; if (!Directory.Exists(desfolderdir)) { Directory.CreateDirectory(desfolderdir); } File.Copy(file, srcfileName,true); } } } }
备注:目录不存在的会自动创建