1

How can I download multiple files, one by one. Using my code.

        //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();
    }

Because I need to update the checkpoint file, after you download the Game.

I looked at several topics, but without success. Thank everyone who helped me ... Sorry for my bad English

Thank you to all that help me. I will be very grateful...

1 Answers1

0

If performance is not an issue, you can use DownloadFile and then it will download them in the sequence you specify and you don't need to bother with all the overlapping asynchronous operations. Since you're only downloading 3 files, the time savings of async coding is probably not worth it.

Alternately you could define flags for each file you intend to download (at the class level, not inside any of your functions), which could be in a Dictionary or just 3 bool variables. In DownloadFilAsync documentation note the userToken argument, which you use to identify which of the 3 files is done downloading. Use that to set the completed flags; then when the last download is complete, you carry on with whatever happens next.

Star Ford
  • 68
  • 5