/// <summary> /// Removes child file /// </summary> /// <param name="name">File name</param> /// <exception cref="InvalidOperationException"> /// Throws if child item is not a file, or if the item kind is not volume or directory, /// or if a child item with the specified name is not exists, /// or if a child item with the specified name is locked /// </exception> public void RemoveChildFile(string name) { this.ValidateCanHasChildItems(); IFileSystemItem file = FileSystemItemFactory.CreateFile(name); // validate name IFileSystemItem child = this.ChildItems.Where(item => FileSystemItemNameComparer.Equals(file.Name, item.Name)).FirstOrDefault(); if (child is null) { throw new InvalidOperationException("File with the specified name is not exists."); } if (child.Kind != FileSystemItemKind.File) { throw new InvalidOperationException("Item with the specified name is not a file."); } if (child.IsLocked) { throw new InvalidOperationException("File with the specified name is locked."); } this.childItems.Remove(child); }
/// <summary> /// Removes empty child directory /// </summary> /// <param name="name">Directory name</param> /// <exception cref="InvalidOperationException"> /// Throws if child item is not a directory, or if child directory is not empty, /// or if the item kind is not volume or directory, or if a child item with the specified name is not exists /// </exception> public void RemoveChildDirectory(string name) { this.ValidateCanHasChildItems(); IFileSystemItem directory = FileSystemItemFactory.CreateDirectory(name); // validate name IFileSystemItem child = this.ChildItems.Where(item => FileSystemItemNameComparer.Equals(directory.Name, item.Name)).FirstOrDefault(); if (child is null) { throw new InvalidOperationException("Directory with the specified name is not exists."); } if (child.Kind != FileSystemItemKind.Directory) { throw new InvalidOperationException("Item with the specified name is not a directory."); } if (child.ChildItems.Count > 0) { throw new InvalidOperationException("Directory with the specified name is not empty."); } this.childItems.Remove(child); }
/// <summary> /// Adds child file /// </summary> /// <param name="name">File name</param> /// <exception cref="InvalidOperationException"> /// Throws if child directory or file with same name already exists, /// or if the item kind is not volume or directory /// </exception> /// <returns>Added child file</returns> public IFileSystemItem AddChildFile(string name) { this.ValidateCanHasChildItems(); IFileSystemItem file = FileSystemItemFactory.CreateFile(name); if (this.ChildItems.Any(item => FileSystemItemNameComparer.Equals(file.Name, item.Name))) { throw new InvalidOperationException("Directory or file with the specified name already exists."); } this.childItems.Add(file); return(file); }
/// <summary> /// Adds child item /// </summary> /// <param name="child">Child item</param> /// <exception cref="ArgumentNullException">Throws if the child item is null</exception> /// <exception cref="InvalidOperationException"> /// Throws if the item is not a volume or a directory, or if the child item is not a directory or a file, /// or if the item already have a child with the same name /// </exception> public void AddChild(IFileSystemItem child) { this.ValidateCanHasChildItems(); if (child is null) { throw new ArgumentNullException(nameof(child)); } if (!this.validChildKinds.Contains(child.Kind)) { throw new ArgumentException(this.validChildKindsMessage); } bool IsChildAlreadyExists() => this.ChildItems.Any(item => FileSystemItemNameComparer.Equals(item.Name, child.Name)); if (IsChildAlreadyExists()) { throw new InvalidOperationException("Child item with the specified name already exists."); } this.childItems.Add(child); }