I'm writing an application that manages a library of mods/addons for a game. Every so often, one of these mods has an update available, and the new version is downloaded using WebClient.DownloadFileAsync(), having retrieved the filename through reading the Content-Disposition in a WebRequest response.
When two or more updates are available, the first downloads perfectly fine but when you try to download a second file without having restarted the application WebClient freezes, the file is created with the retrieved name, but contains 0 bytes and neither the DownloadProgressChanged or DownloadFileCompleted events are triggered.
If I do not try to retrieve the original filename, and just give them a name locally, then WebClient doesn't freeze. But I need the original filename.
Is there anything I can do to avoid this issue, while still being able to retrieve the original filename?
private void Download(string url, string downloadDirectory)
{
WebClient wc = new WebClient();
string filename = FilenameFromURL(url);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadComplete);
wc.DownloadFileAsync(new Uri(url), downloadDirectory + filename);
}
private string FilenameFromURL(string url)
{
return new ContentDisposition(WebRequest.Create(url).GetResponse().Headers["Content-Disposition"].ToString()).FileName;
}
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
downloadProgressBar.Value = e.ProgressPercentage;
}
private void DownloadComplete(object sender, AsyncCompletedEventArgs e)
{
// ...
}