我用这种方法下载了一个文件:
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
webClient.DownloadDataAsync(new Uri(this.Url));
这就是我将它保存到磁盘的方式:
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
try
{
if (e.Result != null)
{
string VideoFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Playtube\\VideoCache\\" + this.id + ".wmv";
File.WriteAllBytes(VideoFile, e.Result);
isDownloading = false;
callbackFinish();
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
而且我想知道是否可以下载文件并同时将其保存到磁盘,而不是等到文件完成下载才能保存。