我用这种方法下载文件:

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


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

最佳答案

您可以使用DownloadFileAsync将其直接下载到文件中。

10-07 19:01
查看更多