帮助
我想在新窗口中打开url,bookmarkformt在当前Webbrowser 中打开的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##//
如何检测Url是否为书签格式?正则表达式可以解决这个问题,我不能写一个正确的正则表达式吗?
这是我的解决方案,但需要检测url是书签格式
string htmFilename = @"D:AdministratorDesktop123.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();
}
下面的正则表达式将匹配正确的Bookmark格式
file:///[^.]*.(?:htm|html)#+.*
演示
或
file:///D:/Administrator/Desktop/123.htm#.*
演示