简体   繁体   中英

How can I download multiple files with WebClient in C#?

I can't seem to figure out how to download multiple files using DownloadFileAsync. How can I input a list as a URI?

My current code for a single download looks like this:

        WebClient client = new WebClient();
        client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

        // Starts the download
        client.DownloadFileAsync(new Uri(chosenVersion), tbFolder.Text + chosenVersionFileName);   

'chosenVersion' is just a link, say example.com/some.jpeg.

I just want to get all the downloads bundled into one progress bar while downloading at the same time.

I just want to get all the downloads bundled into one progress bar while downloading at the same time.

An instance of WebClient can handle just one download at a time. You can modify the approach given in

https://stackoverflow.com/a/6992743/141172

to download multiple files in parallel by replacing the queue with a construct such as a List<string> and using Parallel.Foreach() to process the list concurrently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM