Winforms Webbrowser控制URL验证

时间:2014-11-24 16:59:26

标签: c# winforms webbrowser-control

我正在尝试在单击按钮时验证winform Web浏览器控件URL。我想看看网络浏览器的当前网址是否与某个网址匹配。当我尝试运行此代码时程序会冻结

private void button_Click(object sender, EventArgs e)
 {
    // Check to see if web browser is at URL
    if (webBrowser1.Url.ToString != "www.google.com" || webBrowser1.Url.ToString == null)
 { 
    // Goto webpage
    webBrowser1.Url = new Uri("www.google.ca");
 }
 else {
    webBrowser1.Document.GetElementById("first").InnerText = "blah";
    webBrowser1.Document.GetElementById("second").InnerText = "blah";
 }
}

1 个答案:

答案 0 :(得分:0)

你走了。

private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("https://www.google.ca");

            // Check to see if web browser is at URL
            if (webBrowser1.Url != null)
            {
                if (webBrowser1.Url.ToString() != "https://www.google.com" || webBrowser1.Url.ToString() == null)
                {
                    // Goto webpage
                    webBrowser1.Url = new Uri("www.google.ca");
                }
                else
                {
                    webBrowser1.Document.GetElementById("first").InnerText = "blah";
                    webBrowser1.Document.GetElementById("second").InnerText = "blah";
                }
            }

        }

1)请使用带有URL的架构。

2)使用ToString()作为函数。