public virtual async Task DownloadDirectoryAsync( string localPath, string storagePath, IProgress <StorageProgressDto> progress = default, CancellationToken cancellationToken = default) { CheckPaths(localPath, storagePath); if (!Directory.Exists(localPath)) { Directory.CreateDirectory(localPath); } if (!DirectoryExtensions.IsDirectoryEmpty(localPath)) { throw new IOException($"The directory '{localPath}' is not empty."); } try { var(directories, files) = await _storageProvider.ListFileEntriesAsync(storagePath, progress, cancellationToken) .ConfigureAwait(false); if (directories.Any()) { await _directoryService.CreateDirectoriesAsync(localPath, directories, progress, cancellationToken) .ConfigureAwait(false); } if (files.Any()) { await _fileService.CreateFilesAsync(localPath, storagePath, files, progress, cancellationToken) .ConfigureAwait(false); } } catch (Exception) { await _directoryService.DeleteDirectoryAsync(localPath, progress).ConfigureAwait(false); throw; } }