public static async Task DownloadDirectoryAsync(this IHosting hosting, UPath source, DirectoryInfo destination, IProgress <UPath> failures = null, IProgress <UPath> successes = null) { if (File.Exists(destination.FullName)) { throw new UnexpectedItemType("Destination should be a directory"); } if (destination.Parent == null || File.Exists(destination.Parent.FullName) || !destination.Parent.Exists) { throw new UnexpectedItemType("Problem with destination parent"); } if (!destination.Exists) { Directory.CreateDirectory(destination.FullName); } var tasks = new List <Task>(); var items = await hosting.GetDirectoryListAsync(source); foreach (var item in items) { var destFile = destination.GetSubFile(item.Name); var destDir = destination.GetSubDirectory(item.Name); var task = item.IsFile ? hosting.DownloadFileAsync(item.Path, destFile) : hosting.DownloadDirectoryAsync(item.Path, destDir, failures, successes); task = task.ContinueWith(t => (t.IsFaulted ? failures : successes).Report(item.Path)); tasks.Add(task); } await Task.WhenAll(tasks); }
public static async Task DownloadFileAsync(this IHosting hosting, UPath source, FileInfo destination, IProgress <double> progress = null) { using (var stream = destination.OpenWrite()) { await hosting.DownloadFileAsync(stream, source, progress); } }