/// <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 directory /// </summary> /// <param name="name">Directory 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 directory</returns> public IFileSystemItem AddChildDirectory(string name) { this.ValidateCanHasChildItems(); IFileSystemItem directory = FileSystemItemFactory.CreateDirectory(name); if (this.ChildItems.Any(item => FileSystemItemNameComparer.Equals(directory.Name, item.Name))) { throw new InvalidOperationException("Directory or file with the specified name already exists."); } this.childItems.Add(directory); return(directory); }