C# 使用windows从服务器下载文件

C# 使用windows从服务器下载文件,c#,C#,我在服务器中有一个大小为164MB的exe文件,用于安装sccm包。我正在尝试使用WebClient的Async方法将此文件下载到客户端计算机 该文件大部分时间是部分下载的。因此安装失败。在下面添加我的代码 附言: 守则: private static Boolean DownloadFile(string HostPath,string Filepath) { WebClient webClient = new WebClient(); webClient.DownloadF

我在服务器中有一个大小为164MB的exe文件,用于安装sccm包。我正在尝试使用
WebClient
Async
方法将此文件下载到客户端计算机

该文件大部分时间是部分下载的。因此安装失败。在下面添加我的代码

附言:

守则:

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

此问题是否与
异步
功能有关?

要下载文件,我正在使用此代码,它对我有效

    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!");
    }
看一看


可能是Hi的重复,而不是异步,我使用下载文件,它现在工作正常。这是一个选项。但我认为你需要异步操作。如果您想下载文件而不是阻止用户界面,上述解决方案将对您非常有用。查看提供的链接。似乎你的问题是meas引导。感谢Ahmed提供的漂亮而干净的代码,但我想知道的是:1。文件将保存在本地的哪个位置?2.哪个服务器的Uri?例如,我将ABC应用程序作为ABC.com托管在XYZ服务器中,XYZ.net可以访问该应用程序。URI参数ABC.com或XYZ.net应该是什么?非常感谢!
    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!");
    }
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
}