我試圖下載多個文件,但它沒有按照我的希望工作。 有人能告訴我這個腳本有什麼問題嗎,因爲我已經嘗試了很多東西,真的不知道該怎麼做了。下載多個文件WebClient
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());
}
}
答案就在這裏 http://stackoverflow.com/questions/6992553/how-do-i-async-download-multiple-files-using-webclient-but-one-at-a-time – 2012-07-11 01:36:02
@GX。我已經嘗試過,但無法完成它的工作:(。 – 2012-07-11 01:39:13
你的代碼在哪裏準確地打破?你調試了嗎? – 2012-07-11 01:41:53