/// <summary> /// Copies a directory /// </summary> /// <param name="CopyFrom">Source and Destination</param> public static void Copy(DirCopyInfo CopyFrom) { DirCopyInfo Info = CopyFrom; if (!Directory.Exists(Info.Source)) { throw new FileNotFoundException(); } if (!Directory.Exists(Info.Destination)) { //Console.WriteLine("Creating Directory: " + Info.Destination); Directory.CreateDirectory(Info.Destination); } string[] DirFiles; string[] DirDirs; try { DirFiles = Directory.GetFiles(Info.Source); DirDirs = Directory.GetDirectories(Info.Source); } catch { throw new FileNotFoundException(); } foreach (string SingleDir in DirDirs) { string DirName = "\\"; DirName += SingleDir.Split('\\')[SingleDir.Split('\\').Length - 1]; DirCopyInfo NextInfo = new DirCopyInfo(); NextInfo.BaseDir = Info.BaseDir; NextInfo.Destination = Info.Destination + DirName; NextInfo.Source = SingleDir; Copy(NextInfo); } foreach (string SingleFile in DirFiles) { try { string FileName = SingleFile.Split('\\')[SingleFile.Split('\\').Length - 1]; File.Copy(SingleFile, Info.Destination + "\\" + FileName, Info.OverWrite); } catch { return; } } }
public static void Copy(CopyParameter cp) { CopyParameter Info = cp; if (!Directory.Exists(Info.Source)) { throw new FileNotFoundException(); } if (!Directory.Exists(Info.Destination)) { Directory.CreateDirectory(Info.Destination); } string[] DirFiles; string[] DirDirs; try { DirFiles = Directory.GetFiles(Info.Source); DirDirs = Directory.GetDirectories(Info.Source); } catch { throw new FileNotFoundException(); } foreach (string SingleDir in DirDirs) { bool isIgnore = false; if (cp.IgnoreFolders != null) { foreach (string iFolder in cp.IgnoreFolders) { if (SingleDir.ToLower().IndexOf(string.Concat("\\", iFolder.ToLower())) > 0) { isIgnore = true; break; } } } if (isIgnore) { continue; } string DirName = "\\"; DirName = string.Concat(DirName, SingleDir.Split('\\')[SingleDir.Split('\\').Length - 1]); CopyParameter NextInfo = new CopyParameter(); NextInfo.Destination = string.Concat(Info.Destination, DirName); NextInfo.Source = SingleDir; NextInfo.IgnoreFolders = cp.IgnoreFolders; NextInfo.IsOverwrite = cp.IsOverwrite; Copy(NextInfo); } foreach (string SingleFile in DirFiles) { try { string FileName = SingleFile.Split('\\')[SingleFile.Split('\\').Length - 1]; string destFileName = string.Concat(Info.Destination, "\\", FileName); if (!Info.IsOverwrite && File.Exists(destFileName)) { continue; } File.Copy(SingleFile, destFileName, Info.IsOverwrite); } catch (Exception ex) { LogBuilder.NLogger.Error(ex); //throw ex; } } }