我在寫一個管理遊戲的mods/addons庫的應用程序。每隔一段時間,這些mod中的一個都有更新可用,並且使用WebClient.DownloadFileAsync()下載新版本,通過讀取WebRequest響應中的Content-Disposition來檢索文件名。當異步下載第二個文件時WebClient凍結,如果我從Content-Disposition獲取原始文件名
當兩次或多次更新可用時,第一次下載完全正常,但是當您嘗試下載第二個文件而未重新啓動應用程序時,WebClient會凍結,該文件將使用檢索到的名稱創建,但包含0個字節, DownloadProgressChanged或DownloadFileCompleted事件被觸發。
如果我不嘗試檢索原始文件名,並且只給它們一個本地名稱,則WebClient不會凍結。但我需要原始文件名。
有什麼我可以做的,以避免這個問題,同時仍然能夠檢索原始文件名?
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)
{
// ...
}
你確定你沒有吞下異常嗎?你厭倦了一些HTTP調試嗎?舉例來說,小提琴手? –