How to download multiple files in C#

By FoxLearn 7/16/2024 9:27:21 AM   11.02K
Downloading multiple files in parallel in a C# Windows Forms Application can be achieved using asynchronous programming techniques.

Here's a step-by-step guide on how to download a file from a URL in C#.

Create a new Windows Forms Application in Visual Studio, then open your form designer. Next, You need to drag and drop the TextBox, ProgressBar, Label and Button controls from the Visual Studio Toolbox to your winform.

We will download a file from a url when you enter the url into the TextBox control. If you want to download multiple files, you need to set the TextBox to allow multiple lines, then you just need to use the loop to download each file.

We will create a simple UI that allows downloading multiple files in c# as shown below.

c# download multiple files

C# Download multiple files with progress bar

You can easily create a c# download file from server using the WebClient class without using any third party libraries.

Adding a click event handler to the Download button allows you to download multiple files concurrently in c#.

private async void btnDownload_Click(object sender, EventArgs e)
{
    using (FolderBrowserDialog fbd = new FolderBrowserDialog() { Description = "Select folder..." })
    {
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            List<string> list = new List<string>(txtUrl.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
            foreach (string url in list)
            {
                using (WebClient client = new WebClient())
                {
                    progressBar.Value = 0;
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;
                    client.DownloadProgressChanged += Wc_DownloadProgressChanged;
                    Uri uri = new Uri(url);
                    string filename = System.IO.Path.GetFileName(uri.LocalPath);
                    await client.DownloadFileTaskAsync(uri,
                        $"{fbd.SelectedPath}\\{filename}"
                    );
                }
            }
            MessageBox.Show("You have finished downloading the file.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

First, We will use FolderBrowserDialog to help you choose the path where you want to save the downloaded file, then make a c# download file from url to folder that you selected.

Adding a DownloadProgressChanged event handler allows you to update ProgressBar and Label controls.

private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar.Value = e.ProgressPercentage;
    progressBar.Update();
    lblStatus.Invoke((MethodInvoker)(() => lblStatus.Text = $"{e.ProgressPercentage}%"));
}

To update the value to Label control in another thread you should use the Invoke method, otherwise you will encounter an error.

Through this example, you have learned how to download multiple files in parallel with progress bar in a C# Windows Forms Application using asynchronous programming techniques.