/// <summary> /// Creates a copy of the file in the specified path /// This method also specifies what to do if a file with the same name already exists. /// </summary> /// <param name="destinationFullPath"> /// The destination path where the copy of the file is created. /// </param> /// <param name="option"> /// One of the <see cref="Enum"/> values that determines how to handle the collision if a file /// with the specified "<see cref="destinationFullPath"/>" already exists. /// </param> /// <returns> /// <see cref="IFile"/> that represents the copy /// of the file that path of it, is "<see cref="destinationFullPath"/>". /// </returns> public new IFile Copy(string destinationFullPath, NameCollisionOption option = NameCollisionOption.FailIfExists) { if (string.IsNullOrWhiteSpace(destinationFullPath)) { throw new ArgumentNullException(nameof(destinationFullPath)); } switch (option) { case NameCollisionOption.GenerateUniqueName: Base.StorageHelper.Init(); var item = Core.Base.StorageHelper.Creat().GetItem(destinationFullPath); if (item != null) { return(Copy(item.Parent, NextName.ProductNextName(PureName) + Extension, option)); } else { return(new File(BaseStorageItem.CopyTo(destinationFullPath))); } case NameCollisionOption.ReplaceExisting: return(new File(BaseStorageItem.CopyTo(destinationFullPath, true))); case NameCollisionOption.FailIfExists: return(new File(BaseStorageItem.CopyTo(destinationFullPath, false))); default: throw new ArgumentException("invalid option", nameof(option)); } }
public new IFolder Copy(string destinationFullPath, NameCollisionOption option = NameCollisionOption.FailIfExists) { if (string.IsNullOrWhiteSpace(destinationFullPath)) { throw new ArgumentNullException(nameof(destinationFullPath)); } if (!Exist) { throw new SysIO.DirectoryNotFoundException("Source folder is not exist"); } Base.StorageHelper.Init(); var item = Core.Base.StorageHelper.Creat().GetItem(destinationFullPath); if (item != null) { if (!string.Equals(Path, item.Path, StringComparison.OrdinalIgnoreCase)) { switch (option) { case NameCollisionOption.GenerateUniqueName: return(Copy(item.Parent.Path, NextName.ProductNextName(item.Name), option)); case NameCollisionOption.ReplaceExisting: item.Delete(); break; case NameCollisionOption.FailIfExists: throw new SysIO.IOException($"A {item.Type} exist in {destinationFullPath}"); default: throw new ArgumentException("invalid option", nameof(option)); } } else { throw new SysIO.IOException("Source and destination folders are same"); } } var destFolder = new Folder(destinationFullPath); destFolder.Create(); var files = GetFiles(); foreach (var file in files) { file.Copy(destFolder); } var subFolders = GetFolders(); foreach (var folder in subFolders) { folder.Copy(destFolder); } return(destFolder); }
/// <summary> /// Moves the current file to <see cref="destinationFullPath"/>. /// This method also specifies what to do if a file with the /// same name already exists in the specified folder. /// </summary> /// <param name="destinationFullPath"> /// new full path of file, including name and extension. /// </param> /// <param name="option"> /// An <see cref="Enum"/> value that determines how responds if a file with same path /// is exist in the destination folder. /// </param> public override void Move(string destinationFullPath, NameCollisionOption option = NameCollisionOption.FailIfExists) { if (string.IsNullOrWhiteSpace(destinationFullPath)) { throw new ArgumentNullException(nameof(destinationFullPath)); } if (!Exist) { // like this, we will have a real exception XD. (FileNotFoundException) BaseStorageItem.MoveTo(destinationFullPath); } Base.StorageHelper.Init(); var item = Core.Base.StorageHelper.Creat().GetItem(destinationFullPath); if (item != null) { if (!string.Equals(Path, item.Path, StringComparison.OrdinalIgnoreCase)) { switch (option) { case NameCollisionOption.GenerateUniqueName: Move(item.Parent, NextName.ProductNextName(Name), option); return; case NameCollisionOption.ReplaceExisting: item.Delete(); //No problem. Existence of source checked in start of method. break; case NameCollisionOption.FailIfExists: // System.IO default option. .net will throw exception break; default: throw new ArgumentException("invalid option", nameof(option)); } } } BaseStorageItem.MoveTo(destinationFullPath); ResetParent(); }
public override void Move(string destinationFullPath, NameCollisionOption option = NameCollisionOption.FailIfExists) { if (string.IsNullOrWhiteSpace(destinationFullPath)) { throw new ArgumentNullException(nameof(destinationFullPath)); } if (!Exist) { throw new SysIO.DirectoryNotFoundException("Source folder is not exist"); } Base.StorageHelper.Init(); var item = Core.Base.StorageHelper.Creat().GetItem(destinationFullPath); if (item != null) { if (!string.Equals(Path, item.Path, StringComparison.OrdinalIgnoreCase)) { switch (option) { case NameCollisionOption.GenerateUniqueName: Move(item.Parent.Path, NextName.ProductNextName(item.Name), option); return; case NameCollisionOption.ReplaceExisting: item.Delete(); break; case NameCollisionOption.FailIfExists: throw new SysIO.IOException($"A {item.Type} exist in {destinationFullPath}"); default: throw new ArgumentException("invalid option", nameof(option)); } } else { if (string.Equals(Path, item.Path, StringComparison.Ordinal)) { throw new SysIO.IOException("Source and destination folders are same"); } else { Move(item.Parent, NextName.ProductNextName(Name), NameCollisionOption.GenerateUniqueName); Move(destinationFullPath); return; } } } try { BaseStorageItem.MoveTo(destinationFullPath); } catch (SysIO.IOException) { var newOne = Copy(destinationFullPath, option) as Folder; Delete(); base.BaseStorageItem = newOne.BaseStorageItem; } ResetParent(); }