2014-02-08 29 views
-1

代码:如何下载所有htmls并将它们保存为一个循环?

client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#", localFilename + "\\Sat24_Rain_Europe.html"); 
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=2&continent=europa#", localFilename + "\\Sat24_Wind_Europe.html"); 
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=3&continent=europa#", localFilename + "\\Sat24_Lightnings_europe.html"); 
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=0&continent=europa#", localFilename + "\\Sat24_Temperature_Europe.html"); 
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=4&continent=europa#", localFilename + "\\Sat24_Cloudtypes_Europe.html"); 

但每次代替下载到使一个循环,将通过诸如HTMLS运行:

for (int i = 0; i < 4; i++) 
         { 
          client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=i&continent=europa#", localFilename + "\\Sat24_Cloudtypes_Europe.html"); 
         } 

相反0 1 2 3 4 ...使用(i)在循环。

但是,如果我改变client.DownloadFile( “http://www.sat24.com/foreloop.aspx?type=4&continent=europa#”,localFilename + “\ Sat24_Cloudtypes_Europe.html”);

数字4给我所以变量我会像蓝色的一部分,而不是一个变量。

回答

2

像这样:

var files = new Dictionary<int, string> { 
    { 0, "Sat24_Temperature_Europe.html" }, 
    { 1, "Sat24_Rain_Europe.html" }, 
    { 2, "Sat24_Wind_Europe.html" }, 
    { 3, "Sat24_Lightnings_europe.html" }, 
    { 4, "Sat24_Cloudtypes_Europe.html" } 
}; 

const string urlFormat = 
        "http://www.sat24.com/foreloop.aspx?type={0}&continent=europa#"; 
foreach (var kv in files) 
{ 
    string url = string.Format(urlFormat, kv.Key); 
    client.DownloadFile(url, localFilename + "\\" + kv.Value); 
} 
相关问题