0
public partial class Form1 : Form
{
    public class Filex
    {
        public string fileName { get; set; }
        public string DownLoadString { get; set; }
        public bool Downloaded { get; set; }
    }
    public Form1()
    {
        InitializeComponent();
    }
    WebClient DownloadClient = new WebClient();
    List<Filex> FilesList = new List<Filex>();
    public void StartDownload()
    {

        Filex FileName1 = new Filex();
        FileName1.fileName = "AwesomeFile1.rar";
        FileName1.DownLoadString = "http://download.thinkbroadband.com/5MB.zip";
        Filex FileName2 = new Filex();
        FileName2.fileName = "AwesomeFile2.rar";
        FileName2.DownLoadString = "http://download.thinkbroadband.com/5MB.zip";
        FilesList.Add(FileName1);
        FilesList.Add(FileName2);

        foreach (var File in FilesList)
        {
            DownloadFile(File.fileName, File.DownLoadString);
        }

    }

    public void DownloadFile(string FileName, string FileURL)
    {
        string ApplicationPath = Path.GetDirectoryName(Application.ExecutablePath);

        string DownloadLocation = ApplicationPath + "\\TempFiles";
        bool exists = System.IO.Directory.Exists(DownloadLocation);
        if (!exists)
        {
            System.IO.Directory.CreateDirectory(DownloadLocation);
        }
        DownloadClient = new WebClient();

        Uri FileUriLink = new Uri(FileURL);
        DownloadClient.DownloadProgressChanged += DownloadClient_DownloadProgressChanged;
        DownloadClient.DownloadFileAsync(FileUriLink, DownloadLocation + FileName);

    }





    private void DownloadClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {

        string MiaxSize = GetFileSize(e.TotalBytesToReceive);
        string MinSize = GetFileSize(e.BytesReceived);
        FileSize_lbl.Text = MiaxSize + "/" + MinSize;


        progressBar1.Maximum = ((int)e.TotalBytesToReceive / 100);
        progressBar1.Value = ((int)e.BytesReceived / 100);
        if (progressBar1.Value == progressBar1.Maximum)
        {
            UpdateState_lbl.Text = "Complete...";
        }
        else
        {
            UpdateState_lbl.Text = "Downloading...";
        }
    }
    private string GetFileSize(double byteCount)
    {
        string size = "0 Bytes";
        if (byteCount >= 1073741824.0)
            size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
        else if (byteCount >= 1048576.0)
            size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB";
        else if (byteCount >= 1024.0)
            size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB";
        else if (byteCount > 0 && byteCount < 1024.0)
            size = byteCount.ToString() + " Bytes";

        return size;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        StartDownload();
    }


}

Hi everyone, well the current code does the job but not the way i want, it downloads both files at the same time and reports to progress bar both at same time... i would like to make it a queue and report each file seperatly one after another finishes

tried with background workers but it makes it even more horrible because it requiers delegates to change nececerry labels, file name..size...1 out of how many files etc...

could anyone help with a proper file download solution please

Extzy
  • 19
  • 3
  • Put your Downloadfile into threads, or background workers.. – BugFinder Nov 10 '16 at 14:40
  • The question is too broad for SO as there are lots of possible ways to do this. You probably want to add your downloads into some kind of collection like a `Queue` and pop one off each time a download completes until the collection is empty. – Equalsk Nov 10 '16 at 14:45
  • but than it will still download them both at same time, How exactly it is suposed to help? – Extzy Nov 10 '16 at 14:45
  • yes equalsk thats the case but but how do i make it a queue which activates 1 by one and not all at once? – Extzy Nov 10 '16 at 14:46
  • There are lots of ways, [check out this thread for an example using Queue](http://stackoverflow.com/questions/6992553/how-do-i-async-download-multiple-files-using-webclient-but-one-at-a-time). – Equalsk Nov 10 '16 at 15:01

0 Answers0