WebBrowser 关闭文件下载对话框

WebBrowser close file download dialog box

我正在使用 windows.Forms.WebBrowser C# 下载文件,在我 运行 InvokeScript 之后,我使用 Application.DoEvents 获取我的文件以前进到我的下一个文件,但下载对话框保持不变打开后我卡在 Application.DoEvents,什么也做不了。

如何取消或关闭此下载对话框?

我的代码:

     HtmlElement head = webBrowser.Document.GetElementById("btnExcelExport");
     HtmlElement scriptEl = webBrowser.Document.CreateElement("script");
     IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
     element.text = "function DownloadFile() {$('#btnExcelExport').click()}";
     head.AppendChild(scriptEl);
     webBrowser.Document.InvokeScript("DownloadFile");

     HtmlElementCollection iFrames =     webBrowser.Document.GetElementsByTagName("iframe");
                for (int i = 0; i < iFrames.Count; i++)
                {
                    string src = iFrames[i].GetAttribute("src");
                    if (src.Contains("Export"))
                    {
                        txt_url.Text = "https://test.com" + src;

                        string cooki = webBrowser.Document.Cookie;
                        int response = URLDownloadToFile(0, "https://test.com" + src, @"C:\temp\Test.txt", 0, 0);

                    }

                }
                Application.DoEvents();
//....got to next file

我知道我的问题是错误的,以及为什么我被低估了,但无论如何我找到了这个解决方案。

您需要使用 Navigating 事件并检查您是否在下载中 url 使用取消下载 "e.Cancel = true"

private void webBrowser_Navigating(object sender,WebBrowserNavigatingEventArgs e)
        {

            if (e.Url.ToString().Contains("Download"))
            {
                e.Cancel = true;
            }
            }