0

I have a single exe file of size 164 MB in server for installing an sccm package. I am trying to download this file to client machine using a WebClient's Async method.

The file is partially downloading most of the time. The installation fails because of it. Adding my code below.

PS:

hostpath="https://naagentswhk.cognizant.com/US_IBCM_InstallerV1.exe"
filepath="D:\Users\417193\AppData\Local\SupportSoft\expertouchent\417193\exec"

The code:

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

Is this issue related to the Async function?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
jerin
  • 71
  • 1
  • 3
  • 9
  • possible duplicate of [Download multiple files async and wait for all of them to finish before executing the rest of the code](http://stackoverflow.com/questions/16514027/download-multiple-files-async-and-wait-for-all-of-them-to-finish-before-executin) – Backs Aug 10 '15 at 03:54

2 Answers2

1

To download a file i am using this code and it's working for me

    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!");
    }
Ahmed Mandour
  • 330
  • 1
  • 8
  • Thanks Ahmed for the nice and clean code but I wonder for few things as: 1. In which location file will be saved locally? 2. Uri of which server? E.g. I have ABC application hosted as abc.com in XYZ server, that can be accessible by xyz.net What should be the URI parameter abc.com or xyz.net? Many thanks! – Navin Pandit Jan 20 '19 at 02:13
1

Take a look 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
}
Community
  • 1
  • 1
Artiom
  • 7,694
  • 3
  • 38
  • 45
  • Hi,Instead of Async i used Download file and its working fine now. – jerin Aug 11 '15 at 06:53
  • it's an option. But I thought you need async operation. Solution above will be useful to you in case you want to download the file and not to block UI. Look at provided link. Seems like your question was meas leading. – Artiom Aug 11 '15 at 11:01