2016-09-18 2 views
0

여러 파일을 하나씩 다운로드하려면 어떻게해야합니까? 내 코드 사용.C에서 DownloadFileAsync를 사용하여 하나씩 여러 파일 다운로드

 //Download File 
    public void DownloadFile(string url, string folderfile) 
    { 
     WebClient wc = new WebClient(); 
     try 
     { 
      wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted); 
      wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadChanged); 

      wc.DownloadFileAsync(new Uri(url), folderfile); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: \n\n" + ex.Message); 
     } 
    } 

    private void DownloadChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     double bytesIn = double.Parse(e.BytesReceived.ToString()); 
     double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); 
     double percentage = bytesIn/totalBytes * 100.0; 
     int percente = int.Parse(Math.Truncate(percentage).ToString()); 

     PBar.Value = percente; 

     progress.Text = percente + " %"; 
     size.Text = string.Format("{0} MB/{1} MB", (e.BytesReceived/1024d/1024d).ToString("0.00"), (e.TotalBytesToReceive/1024d/1024d).ToString("0.00")); 
    } 

    private void DownloadCompleted(object sender, AsyncCompletedEventArgs e) 
    { 
     if (e.Error != null) 
     { 
      MessageBox.Show("Erro: " + e.Error.Message); 
     } 
     else 
     { 
      info.Text = "Download Success."; 
     } 
    } 

    public void CheckFileDownload() 
    { 
     string urlGame = "http://localhost/"; 
     string Game = "Game.exe"; 
     string Support = "Support.Xml"; 
     string Info = "Info.xml"; 
     if (!File.Exists(Game)) 
     { 
      //Download Game 
      DownloadFile(urlGame + Game, Game); 
      info.Text = "Downloading: " + Game; 

      //If the game is downloading full 
      DownloadFile(urlGame + Info, Info); 
      DownloadFile(urlGame + Support, Support); 
      info.Text = "Updating information..."; 
     } 
    } 

    private void Launcher_Load(object sender, EventArgs e) 
    { 
     CheckFileDownload(); 
    } 

게임을 다운로드 한 후 검사 점 파일을 업데이트해야하기 때문에.

여러 주제를 살펴 보았지만 성공하지 못했습니다. 도와 주신 모든 분들께 감사드립니다 ... 나쁜 영어로 죄송합니다.

감사합니다. 나는 매우 감사 할 것입니다 ...

+0

처음부터 다운로드 링크가 모두 있습니까? – Niklas

+0

안녕하세요. 네, 있어요! –

+0

문제를 해결하는 데 도움이 되었습니까? – Niklas

답변

0

성능이 문제가되지 않으면 DownloadFile을 사용할 수 있으며 지정된 순서대로 다운로드하므로 중복되는 비동기 작업을 모두 신경 쓰지 않아도됩니다. 3 파일 만 다운로드하기 때문에 비동기 코딩 시간을 절약 할 가치가 없을 것입니다.

또는 (클래스 수준에서 함수가 아닌) 다운로드하려는 각 파일에 대한 플래그를 정의 할 수 있습니다.이 플래그는 사전 또는 3 개의 bool 변수에있을 수 있습니다. DownloadFilAsync documentation에서 3 개의 파일 중 다운로드가 완료된 것을 식별하는 데 사용하는 userToken 인수를 확인하십시오. 이 플래그를 사용하여 완료 플래그를 설정하십시오. 마지막 다운로드가 완료되면 다음에 어떤 작업이든 계속 수행 할 수 있습니다.

+0

왜냐하면 DownloadFile은 내 양식을 잠그기 때문입니다. –

관련 문제