コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        /// <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);
        }