0

这个 foreach 循环检查网页并查看是否有任何图像然后下载它们。我该如何阻止它?当我按下按钮时,它会永远继续循环。

 private void button1_Click(object sender, EventArgs e)
    {
        WebBrowser browser = new WebBrowser();
        browser.DocumentCompleted +=browser_DocumentCompleted;
        browser.Navigate(textBox1.Text);           
    }

    void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser = sender as WebBrowser;
        HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
        WebClient webClient = new WebClient();

        int count = 0; //if available
        int maximumCount = imgCollection.Count;
        try
        {
                foreach (HtmlElement img in imgCollection)
                {
                    string url = img.GetAttribute("src");
                    webClient.DownloadFile(url, url.Substring(url.LastIndexOf('/')));

                     count++;
                     if(count >= maximumCount)
                          break;
                }
        }
        catch { MessageBox.Show("errr"); }
    }
4

3 回答 3

0

您没有无限循环,您有一个基于您将文件写入磁盘的方式而引发的异常

private void button1_Click(object sender, EventArgs e)
{
    WebBrowser browser = new WebBrowser();
    browser.DocumentCompleted += browser_DocumentCompleted;
    browser.Navigate("www.google.ca");
}

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
    WebClient webClient = new WebClient();

    foreach (HtmlElement img in imgCollection)
    {
        string url = img.GetAttribute("src");
        string name = System.IO.Path.GetFileName(url);
        string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
        webClient.DownloadFile(url, path);
    }
}

该代码在我的环境中运行良好。您似乎遇到的问题是,当您设置 时DownloadFile filepath,您将其设置为类似 `\myimage.png' 的值,并且 webclient 找不到路径,因此它抛出异常。

上面的代码使用扩展名将其放入当前目录。

于 2013-07-26T22:10:31.797 回答
0

如果页面刷新,则事件 browser.DocumentCompleted 可能会导致错误再次触发。您可以尝试注销该事件。

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{    
    WebBrowser browser = sender as WebBrowser;

    browser.DocumentCompleted -= browser_DocumentCompleted;

    HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
    WebClient webClient = new WebClient();

    foreach (HtmlElement img in imgCollection)
    {
        string url = img.GetAttribute("src");
        string name = System.IO.Path.GetFileName(url);
        string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
        webClient.DownloadFile(url, path);
    }
}
于 2013-07-26T23:51:55.633 回答
0

使用break;关键字跳出循环

于 2013-07-26T22:01:20.070 回答