2013-03-04 51 views
2

我下载一个文件,这个方法:WPF下载一个文件,并将其保存在同一时间

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

,我想知道是否可以下载文件并同时将其保存到磁盘,而不是等到文件完成下载后才能保存。

+1

有一个叫'方法WebClient.DownloadFileAsync' http://msdn.microsoft.com/en-US/library/ms144196(v=vs.90).aspx – 2013-03-04 16:34:55

回答

相关问题