【问题标题】:Open local file in System.Windows.Forms.WebBrowser control在 System.Windows.Forms.WebBrowser 控件中打开本地文件
【发布时间】:2013-07-29 14:05:58
【问题描述】:

我在 Windows 窗体 WebBrowser 控件中呈现以下简单的 HTML:

<HTML>
<HEAD></HEAD>
<BODY bottommargin='0' leftmargin='10' rightmargin='0' topmargin='10'>
    <span>Programs</span><br /><br />
    <a href='file:///C:\Temp\prog1.cnc'>Program 1</a><br />
    <a href='file:///C:\Temp\prog2.cnc'>Program 2</a><br />
    <a href='file:///C:\Temp\prog3.cnc'>Program 3</a><br /> 
</BODY>
</HTML>

问题是链接没有导航到 href 属性中标识的文件。

我可以确认控件上的AllowNavigation 属性设置为True

此外,当我单击链接时,Navigating 事件不会触发。

如果我更改路径以引用远程共享上的文件,一切都会按预期工作,例如:

<a href='file://\\servername\Temp\prog1.cnc'>Program 1</a>

或不带file 前缀:

<a href='\\servername\Temp\prog1.cnc'>Program 1</a>

两者都会触发Navigating 事件。

引用本地文件时我遗漏了什么?

我已尝试将文件路径更改为公用文件夹以排除权限问题。 同一个应用程序也在写入文件,因此似乎不太可能出现权限问题。

这些文件是简单的文本文件,我试图在单击链接时在浏览器控件中显示它们。

设置浏览器控件DocumentText属性的代码:

Private Shared Function LoadProgramHtml(ByVal programFiles() As String) As String

    Dim programHtml As New StringBuilder

    If ProgramFiles.Length > 0 Then

        programHtml.AppendLine("<HTML>")
        programHtml.AppendLine("<HEAD></HEAD>")
        programHtml.AppendLine("<BODY bottommargin='0' leftmargin='10' rightmargin='0' topmargin='10'>")
        programHtml.AppendLine("<span>Programs</span><br /><br />")

        For Each program As String In ProgramFiles

            Dim progInfo As New FileInfo(program)

            programHtml.AppendLine(String.Format("<a href='file://{0}'>{1}</a><br />", progInfo.FullName, progInfo.Name.ToUpper))
        Next

        programHtml.AppendLine("</BODY>")
        programHtml.AppendLine("</HTML>")

    End If

    WebViewer.DocumentText = programHtml.ToString()

End Function

处理导航事件的代码:

Private Sub WebViewer_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebViewer.Navigating

    Dim filepath As String = e.Url.OriginalString

    If File.Exists(filepath) Then

        Dim progInfo As New FileInfo(filepath)

        If progInfo.Extension.ToLower = ".cnc" Then

            WebViewer.ScrollBarsEnabled = True
            WebViewer.DocumentText = File.ReadAllText(e.Url.OriginalString).Replace(Chr(13), "<br />")

        End If

        e.Cancel = True

    End If

End Sub

【问题讨论】:

    标签: html vb.net winforms url


    【解决方案1】:

    我找到了一种解决方法,暂时可以让我继续前进,但如果有人对所描述的行为有解释,我仍然会感兴趣。

    通过伪造远程文件路径,我可以像对待实际远程文件一样触发Navigating 事件,然后在Navigating 事件中,我重写路径以重新引用本地文件为如下:

    设置文档文本

    变化:

    programHtml.AppendLine(String.Format("<a href='file://{0}'>{1}</a><br />", progInfo.FullName, progInfo.Name.ToUpper))
    

    收件人:

    programHtml.AppendLine(String.Format("<a href='{0}' target='_top'>{1}</a><br />", progInfo.FullName.ToLower.Replace("c:", "\\faked"), progInfo.Name.ToUpper))
    'note the `\\faked`
    

    处理Navigating 事件

    添加以下内容:

    Dim filepath As String = e.Url.OriginalString.Replace("\\faked", "c:")
    
        If File.Exists(filepath) Then
        ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      相关资源
      最近更新 更多