2012-04-02 61 views
2

我還有一個問題:(
我試圖下載多個文件我的應用程序C#Web客戶端下載多個文件

我的問題是:什麼做我必須做的檢查首先下載完成,然後繼續到第二下載等

這是我的代碼大氣壓:

 private void DownloadBukkit() 
     { 
      MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by..."; 
      webClient.DownloadFileAsync(new Uri(MySingleton.Instance.BukkitDownloadLink), Jar_Location); 
      webClient.DownloadProgressChanged += backgroundWorker1_ProgressChanged; 
      webClient.DownloadFileCompleted += (webClient_DownloadFileCompleted); 
     } 

     private void DownloadDll() 
     { 
      if (!webClient.IsBusy) 
      { 
       MySingleton.Instance.FirstStartProgress = "Downloading HtmlAgilityPack.dll... Please stand by..."; 
       webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation); 
       webClient2.DownloadProgressChanged += backgroundWorker1_ProgressChanged; 
       webClient2.DownloadFileCompleted += (webClient2_DownloadFileCompleted); 
      } 
     } 

    void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
    { 
      DownloadDll(); 
    } 


    void webClient2_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
    { 
     Invoke((MethodInvoker) 
       Close); 
    } 

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      Invoke((MethodInvoker) 
        delegate 
         { 
          labelProgress.Text = MySingleton.Instance.FirstStartProgress; 
          progressBar1.Value = e.ProgressPercentage; 
         }); 
     } 

我已經檢查此鏈接:DownloadFileAsync multiple files using webclient但我真的不知道如何植入這一:(。 (我是qu ite to new to C#)

回答

3

DownloadFileCompleted事件是您知道您已完成下載文件的信號。

從MSDN:

此事件每一個異步文件下載操作 完成次上調。

這不是從你的代碼,在哪裏以及如何Web客戶端和webClient2聲明清楚,但本質上,當第一DownloadFileCompleted事件被觸發,你就可以開始你的第二個下載。但是,請注意,如果您使用WebClient的2個單獨實例,則可以同時執行2個不同文件的下載。

+0

Okai thx!那意味着我只需要在每個DownloadFileCompleted事件中放置一個新的webclient? 編輯:我試圖在DownloadfileCompleted事件中啓動第二次下載,但這似乎並沒有工作。 – 2012-04-02 14:53:58

+0

@ Darkshadw我擴大了一點我的答案。我希望能夠說清楚。 – Icarus 2012-04-02 14:59:14

+0

#Icarus每當我嘗試這樣做: 'WebClient webClient2 = new WebClient(); MySingleton.Instance.FirstStartProgress =「下載DLL ...請支持......」; webClient2.DownloadFileAsync(新Uri(Dll_HtmlAgilityPackUrl),Dll_HtmlAgilityPackLocation);' 它不想開始下載:(。 – 2012-04-02 15:10:28

0

以下是可以根據您的要求更改的快速轉發代碼。

WebClient client = null; 
     public FileDownloader() 
     { 
      InitializeComponent(); 
      client = new WebClient(); 
      client.DownloadProgressChanged += client_DownloadProgressChanged; 
      client.DownloadFileCompleted += client_DownloadFileCompleted; 
     } 

     void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
     { 
      lblMessage.Text = "File Download Compeleted."; 
     } 

     void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      lblMessage.Text = e.ProgressPercentage + " % Downloaded."; 
     } 

     private void StartDownload(object sender, RoutedEventArgs e) 
     { 
      if (client == null) 
       client = new WebClient(); 

//Loop thru your files for eg. file1.dll, file2.dll .......etc. 
      for (int index = 0; index < 10; index++) 
      { 
       //loop on files 
       client.DownloadFileAsync(
        new Uri(
          @"http://mywebsite.com/Files/File" + index.ToString() + ".dll" 
          , UriKind.RelativeOrAbsolute), 
        @"C:\Temp\file+" + index.ToString() + ".dll"); 
      } 

     }