I'm trying to download multiple files but it's not working as I hoped. Can someone tell me what's wrong with this script, because I've tried a lot of things and really don't know what to do anymore.
public static void DownloadFile(string url)
{
WebClient client = new WebClient();
var name = url.Substring(url.LastIndexOf('/')).Remove(0, 1);
foreach (var item in urls)
{
client.DownloadFile(item, "C:\\" + name);
}
}
private void btnGo_Click(object sender, EventArgs e)
{
urls.Add("url1");
urls.Add("url2");
urls.Add("url3");
Parallel.ForEach(urls,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
DownloadFile);
}
using (var sr = new StreamReader(HttpWebRequest.Create(url).GetResponse().GetResponseStream()))
{
using (var sw = new StreamWriter(url.Substring(url.LastIndexOf('/'))))
{
sw.Write(sr.ReadToEnd());
}
}