コード例 #1
0
        /// <summary>
        /// Deletes the specified directory and all its contents.
        /// </summary>
        /// <param name="path">The full or relative path of the directory to delete</param>
        /// <param name="deleteContents">If the directory is not empty, remove its contents</param>
        /// <param name="options">Useful to delete hidden files or dot-files.</param>
        private void DeleteDirInternal(string path, bool deleteContents, FtpListOption options)
        {
            FtpReply reply;

            path = path.GetFtpPath();


#if !CORE14
            lock (m_lock) {
#endif


            // server-specific directory deletion
            if (!path.IsFtpRootDirectory())
            {
                // ask the server handler to delete a directory
                if (ServerHandler != null)
                {
                    if (ServerHandler.DeleteDirectory(this, path, path, deleteContents, options))
                    {
                        return;
                    }
                }
            }


            // DELETE CONTENTS OF THE DIRECTORY
            if (deleteContents)
            {
                // when GetListing is called with recursive option, then it does not
                // make any sense to call another DeleteDirectory with force flag set.
                // however this requires always delete files first.
                var recurse = !WasGetListingRecursive(options);

                // items that are deeper in directory tree are listed first,
                // then files will be listed before directories. This matters
                // only if GetListing was called with recursive option.
                FtpListItem[] itemList;
                if (recurse)
                {
                    itemList = GetListing(path, options);
                }
                else
                {
                    itemList = GetListing(path, options).OrderByDescending(x => x.FullName.Count(c => c.Equals('/'))).ThenBy(x => x.Type).ToArray();
                }

                // delete the item based on the type
                foreach (var item in itemList)
                {
                    switch (item.Type)
                    {
                    case FtpFileSystemObjectType.File:
                        DeleteFile(item.FullName);
                        break;

                    case FtpFileSystemObjectType.Directory:
                        DeleteDirInternal(item.FullName, recurse, options);
                        break;

                    default:
                        throw new FtpException("Don't know how to delete object type: " + item.Type);
                    }
                }
            }


            // SKIP DELETING ROOT DIRS

            // can't delete the working directory and
            // can't delete the server root.
            if (path.IsFtpRootDirectory())
            {
                return;
            }


            // DELETE ACTUAL DIRECTORY

            if (!(reply = Execute("RMD " + path)).Success)
            {
                throw new FtpCommandException(reply);
            }

#if !CORE14
        }
#endif
        }