我正在尝试下载多个文件,但它没有像我希望的那样工作。谁能告诉我这个脚本有什么问题,因为我尝试了很多东西,真的不知道该怎么做。
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());
}
}