禁止WebBrowser mailto

时间:2013-05-13 08:40:19

标签: c# webbrowser-control mailto

我在表单中有一个WebBrowser控件。

我希望当用户点击链接(使用mailto的href)时,它会向网站注册单击该按钮,但它不会打开一个新窗口(Outlook或任何其他网站)。

我找到了这段代码,但它不起作用:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    TextReader tr = File.OpenText(webBrowser1.Url.ToString());
    string  htmlFile = tr.ReadToEnd();
    tr.Close();
    tr.Dispose();

    if (htmlFile.Contains("mailto:"))
    {
        htmlFile = htmlFile.Replace("mailto:", @"mail");

        //Recreate new file with fixed html
        File.Delete(e.Url.LocalPath);
        TextWriter tw = File.CreateText(e.Url.LocalPath);
        tw.Write(htmlFile);
        tw.Flush();
        tw.Close();
        tw.Dispose();

        Refresh();
    }
}

答案不一定是如何修复此代码,如果有更简单的方法吗?会更好。

1 个答案:

答案 0 :(得分:2)

这适用于WP8 webbrowser,但也可能适用于您的情况。 您注册到导航活动。 该事件是可取消的,因此如果您处理事件集e.Cancel = true,则会阻止导航

private void OnNavigating(object sender, NavigatingEventArgs e)
    {
        //take the uri string, not sure is the right method name
        string uri = e.Uri.AbsoluteUri;
        if (uri.StartsWith("mailto"))
            e.Cancel = true;
    }