Z.ExtensionMethods icon indicating copy to clipboard operation
Z.ExtensionMethods copied to clipboard

Add DownloadFileInfoTaskAsync for WebClient

Open adamfisher opened this issue 6 years ago • 2 comments

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));
}

adamfisher avatar Dec 20 '18 14:12 adamfisher

Hello @adamfisher ,

Thank you for reporting,

We will look at it according to the other issue you created.

Best Regards,

Jonathan

JonathanMagnan avatar Dec 21 '18 13:12 JonathanMagnan

@JonathanMagnan Any movement on this?

adamfisher avatar Apr 02 '20 18:04 adamfisher