Ejemplo n.º 1
0
        /// <summary>
        /// Rename the directory
        /// </summary>
        /// <param name="sourcePath">Path to the source directory</param>
        /// <param name="newName">New name of the directory</param>
        /// <returns>A task that represents the completion of the operation</returns>
        protected virtual async Task RenameDirectoryAsync(string sourcePath, string newName)
        {
            string fullSourcePath = GetFullPath(GetVirtualPath(sourcePath));

            string destinationDirectory = _fileProvider.Combine(_fileProvider.GetParentDirectory(fullSourcePath), newName);

            if (GetVirtualPath(sourcePath) == GetRootDirectory())
            {
                throw new Exception(GetLanguageResource("E_CannotRenameRoot"));
            }

            if (!_fileProvider.DirectoryExists(fullSourcePath))
            {
                throw new Exception(GetLanguageResource("E_RenameDirInvalidPath"));
            }

            if (_fileProvider.DirectoryExists(destinationDirectory))
            {
                throw new Exception(GetLanguageResource("E_DirAlreadyExists"));
            }

            try
            {
                _fileProvider.DirectoryMove(fullSourcePath, destinationDirectory);
                await HttpContext.Response.WriteAsync(GetSuccessResponse());
            }
            catch
            {
                throw new Exception($"{GetLanguageResource("E_RenameDir")} \"{sourcePath}\"");
            }
        }