Skip to content

Add DownloadFileInfoTaskAsync for WebClient #18

@adamfisher

Description

@adamfisher

The WebClient class defines DownloadFileTaskAsync as:

public Task DownloadFileTaskAsync(string address, string fileName);

Which is nice but it does not return a reference to the file that was downloaded. An extension method like the following would be helpful (possibly good to do for the non-async version as well):

/// <summary>
/// Downloads the specified resource to a local file as an asynchronous operation using a task object.
/// </summary>
/// <param name="webClient">The web client.</param>
/// <param name="address">The URI of the resource to download.</param>
/// <param name="fileName">The name of the file to be placed on the local computer.</param>
/// <returns>
/// Returns <see cref="System.Threading.Tasks.Task"></see>.
/// The task object representing the asynchronous operation.
/// </returns>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="address">address</paramref> parameter is null.
/// -or-
/// The <paramref name="fileName">fileName</paramref> parameter is null.</exception>
/// <exception cref="T:System.Net.WebException">The URI formed by combining <see cref="System.Net.WebClient.BaseAddress"></see> and <paramref name="address">address</paramref> is invalid.
/// -or-
/// An error occurred while downloading the resource.</exception>
/// <exception cref="T:System.InvalidOperationException">The local file specified by <paramref name="fileName">fileName</paramref> is in use by another thread.</exception>
public static Task<FileInfo> DownloadFileInfoTaskAsync(this WebClient webClient, string address, string fileName)
{
	return webClient.DownloadFileTaskAsync(address, fileName)
		.ContinueWith(task => new FileInfo(fileName));
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions