public void Install(DirectoryInfo source, string destination) { DirectoryXCopyArgs args = new DirectoryXCopyArgs(); args.Source = source; args.Target = new DirectoryInfo(destination); args.DirectoryFilters.Add("*"); args.FileFilters.Add("*"); args.Overwrite = true; args.Rollback = true; args.RollbackTempExtension = ".temp"; DirectoryX.Copy(args); }
/// <summary> /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files /// </summary> /// <param name="diSource">The Source Directory</param> /// <param name="diDestination">The Destination Directory</param> /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param> /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param> /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param> /// <param name="FolderLimit">Iteration Limit - Total Number of Folders/SubFolders to Copy</param> public static void Copy(DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter, bool Overwrite, int FolderLimit) { DirectoryXCopyArgs args = new DirectoryXCopyArgs(); args.Source = diSource; args.Target = diDestination; args.DirectoryFilters.Add((DirectoryFilter != null && DirectoryFilter != string.Empty) ? DirectoryFilter : "*"); args.FileFilters.Add((FileFilter != null && FileFilter != string.Empty) ? FileFilter : "*"); args.Overwrite = Overwrite; args.Limit = FolderLimit; Copy(args); }
///////////////////////////////////////////////////////////////////// /////////////////// The xDirectory.Copy() Method! /////////////////// ///////////////////////////////////////////////////////////////////// public static void Copy(DirectoryXCopyArgs args) { List<DirectoryInfo> diSourceList = new List<DirectoryInfo>(); List<FileInfo> fiSourceList = new List<FileInfo>(); if (args.Source == null) throw new ArgumentException("Source Directory: NULL"); if (args.Target == null) throw new ArgumentException("Target Directory: NULL"); if (!args.Source.Exists) throw new IOException("Source Directory: Does Not Exist"); if (args.Limit < 0) throw new ArgumentException("Folder Limit: Less Than 0"); try { if (args.DirectoryFilters.Count == 0) { args.DirectoryFilters.Add("*"); } if (args.FileFilters.Count == 0) { args.FileFilters.Add("*"); } ///// Add Source Directory to List ///// diSourceList.Add(args.Source); Load(args, diSourceList, fiSourceList); ///// Second Section: Create Folders from Listing ///// foreach (DirectoryInfo di in diSourceList) { if (di.Exists) { string sFolderPath = args.Target.FullName + @"\" + di.FullName.Remove(0, args.Source.FullName.Length); ///// Prevent Silly IOException ///// if (!Directory.Exists(sFolderPath)) Directory.CreateDirectory(sFolderPath); } } ///// Third Section: Copy Files from Listing ///// foreach (FileInfo fi in fiSourceList) { if (fi.Exists) { string sFilePath = args.Target.FullName + @"\" + fi.FullName.Remove(0, args.Source.FullName.Length); ///// Better Overwrite Test W/O IOException from CopyTo() ///// if (args.Overwrite) { if (args.Rollback) { string tmp = sFilePath + args.RollbackTempExtension; if (File.Exists(sFilePath)) { if (File.Exists(tmp)) { File.Delete(tmp); } File.Move(sFilePath, tmp); } try { fi.CopyTo(sFilePath, true); if (File.Exists(tmp)) { try { File.Delete(tmp); } catch { } } } catch (Exception ex) { if (File.Exists(tmp) && false == File.Exists(sFilePath)) { File.Move(tmp, sFilePath); } throw ex; } } else { fi.CopyTo(sFilePath, true); } } else { ///// Prevent Silly IOException ///// if (!File.Exists(sFilePath)) fi.CopyTo(sFilePath, true); } } } } catch { throw; } }
///////////////////////////////////////////////////////////////////// /////////////////// The xDirectory.Copy() Method! /////////////////// ///////////////////////////////////////////////////////////////////// public static void Copy(DirectoryXCopyArgs args) { List <DirectoryInfo> diSourceList = new List <DirectoryInfo>(); List <FileInfo> fiSourceList = new List <FileInfo>(); if (args.Source == null) { throw new ArgumentException("Source Directory: NULL"); } if (args.Target == null) { throw new ArgumentException("Target Directory: NULL"); } if (!args.Source.Exists) { throw new IOException("Source Directory: Does Not Exist"); } if (args.Limit < 0) { throw new ArgumentException("Folder Limit: Less Than 0"); } try { if (args.DirectoryFilters.Count == 0) { args.DirectoryFilters.Add("*"); } if (args.FileFilters.Count == 0) { args.FileFilters.Add("*"); } ///// Add Source Directory to List ///// diSourceList.Add(args.Source); Load(args, diSourceList, fiSourceList); ///// Second Section: Create Folders from Listing ///// foreach (DirectoryInfo di in diSourceList) { if (di.Exists) { string sFolderPath = args.Target.FullName + @"\" + di.FullName.Remove(0, args.Source.FullName.Length); ///// Prevent Silly IOException ///// if (!Directory.Exists(sFolderPath)) { Directory.CreateDirectory(sFolderPath); } } } ///// Third Section: Copy Files from Listing ///// foreach (FileInfo fi in fiSourceList) { if (fi.Exists) { string sFilePath = args.Target.FullName + @"\" + fi.FullName.Remove(0, args.Source.FullName.Length); ///// Better Overwrite Test W/O IOException from CopyTo() ///// if (args.Overwrite) { if (args.Rollback) { string tmp = sFilePath + args.RollbackTempExtension; if (File.Exists(sFilePath)) { if (File.Exists(tmp)) { File.Delete(tmp); } File.Move(sFilePath, tmp); } try { fi.CopyTo(sFilePath, true); if (File.Exists(tmp)) { try { File.Delete(tmp); } catch { } } } catch (Exception ex) { if (File.Exists(tmp) && false == File.Exists(sFilePath)) { File.Move(tmp, sFilePath); } throw ex; } } else { fi.CopyTo(sFilePath, true); } } else { ///// Prevent Silly IOException ///// if (!File.Exists(sFilePath)) { fi.CopyTo(sFilePath, true); } } } } } catch { throw; } }