Search code examples
c#webclient

Webclient C# get filePath after completing async download


I have this method:

public void downloadClip()
        {
            using (WebClient wc = new WebClient())
            {
                //Download
                wc.DownloadFileCompleted += Wc_DownloadFileCompleted;
                string file_name_ender = @"My\Path";
                wc.DownloadFileAsync(new System.Uri(@downloadUri), file_name_ender);
            }
        }

then I have this DownloadFileCompleted:

private void Wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //I need to get where file was downloaded to
        }

I then need to get inside this method string that says where I downloaded my file. Basically my file_name_ender.

Thanks for any help.


Solution

  • Use the second overload method:

    wc.DownloadFileAsync(uri, file_name_ender, file_name_ender);
    

    Then you can get the value from the UserState property in the event:

    private void Wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        var filename = (string)e.UserState;
    }