我在服务器中有一个大小为164 MB的exe文件,用于安装sccm软件包。我正在尝试使用WebClient
Async
方法将此文件下载到客户端计算机。
该文件大部分时间都在部分下载。安装因此失败。在下面添加我的代码。
PS:
hostpath="https://naagentswhk.cognizant.com/US_IBCM_InstallerV1.exe"
filepath="D:\Users\417193\AppData\Local\SupportSoft\expertouchent\417193\exec"
代码:
private static Boolean DownloadFile(string HostPath,string Filepath)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
// webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Downloader_DownloadProgressChanged);
webClient.DownloadFileAsync(new Uri(HostPath), Filepath);
return true;
}
此问题是否与Async
功能相关?
答案 0 :(得分:1)
要下载我正在使用此代码的文件,它正在为我工作
private void button1_Click(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("https://naagentswhk.cognizant.com/US_IBCM_InstallerV1.exe"), @"D:\Users\417193\AppData\Local\SupportSoft\expertouchent\417193\exec\US_IBCM_InstallerV1.exe");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
答案 1 :(得分:1)
看看when and how to use async await
private static async Task DownloadFile(string HostPath,string Filepath)
{
WebClient webClient = new WebClient();
await webClient.DownloadFileAsync(new Uri(HostPath), Filepath);
}
// usage
private async void SomeMethod(){
await DownloadFile("url", "path local');
// it's ready for use.
ReadFile("path local)// File is already downloaded at this point
}