/// <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> /// 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); }