帮助
我要打开新窗口中的url,在当前Webbrowser中打开bookmarkformt url。
我有一个简单的方法来检测书签格式url。
file:///D:/Administrator/Desktop/123.htm#222
包含.htm#或.htm#的
这是正确的书签格式url。
//书签名称为222 file:///D:/Administrator/Desktop/123.htm#222 //书签名为##abc file:///D:/Administrator/Desktop/123.htm###abc //书签名称为//..htm#.htm#abc#.html#// file:///D:/Administrator/Desktop/123.htm#//.HTM##.htm#abc#.html##//
如何检测网址是BookMark格式?RegularExpressions可以解决这个问题,我能不能写一个当前的正则表达式?
这是我的解决方案,但需要检测url是书签格式。
string htmFilename = @"D:\Administrator\Desktop\123.htm";
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
webBrowser1.Navigate(htmFilename);
navigated = true;
}
private void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
textBox1.Text = webBrowser1.StatusText;
}
//webBrowser1 Navigating the first time not open in new window
bool navigated = false;
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//first time nagivate
if (navigated == true)
{
// url is not bookmark format
// open in new window
if (e.Url.ToString().Contains(".htm#") == false)
{
e.Cancel = true;
//webBrowser1.Navigate(e.Url, true);
//Process.Start is bettor then webBrowser1.Navigate(e.Url, true);
//Because when url is directory link such as "file:///C:/"
//webbrowser will open a blank window and then open the Directory
System.Diagnostics.Process.Start(e.Url.ToString());
}
}
textBox2.Text = e.Url.ToString();
}发布于 2014-06-27 05:35:41
下面的regex将匹配正确的书签格式,
file:\/\/\/[^\.]*\.(?:htm|html)#+.*演示
或
file:\/\/\/D:\/Administrator\/Desktop\/123\.htm#.*演示
https://stackoverflow.com/questions/24444555
复制相似问题