Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen local file in System.Windows.Forms.WebBrowser control
    primarykey
    data
    text
    <p>I am rendering the following simple HTML in a Windows Forms WebBrowser control:</p> <pre><code>&lt;HTML&gt; &lt;HEAD&gt;&lt;/HEAD&gt; &lt;BODY bottommargin='0' leftmargin='10' rightmargin='0' topmargin='10'&gt; &lt;span&gt;Programs&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;a href='file:///C:\Temp\prog1.cnc'&gt;Program 1&lt;/a&gt;&lt;br /&gt; &lt;a href='file:///C:\Temp\prog2.cnc'&gt;Program 2&lt;/a&gt;&lt;br /&gt; &lt;a href='file:///C:\Temp\prog3.cnc'&gt;Program 3&lt;/a&gt;&lt;br /&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre> <p>The issue is that the links do not navigate to the file identified in the href attribute.</p> <p>I can confirm that the <code>AllowNavigation</code> property on the control is set to <code>True</code>.</p> <p>Also, the <code>Navigating</code> event does not fire when I click the links.</p> <p>If I change the paths to reference a file on a remote share, everything works as expected, for example:</p> <pre><code>&lt;a href='file://\\servername\Temp\prog1.cnc'&gt;Program 1&lt;/a&gt; </code></pre> <p>OR without the <code>file</code> prefix:</p> <pre><code>&lt;a href='\\servername\Temp\prog1.cnc'&gt;Program 1&lt;/a&gt; </code></pre> <p>Both fire the <code>Navigating</code> event.</p> <p>What am I missing when referencing a local file?</p> <p>I have tried changing the file path to a public folder to rule out permission issues. The same app is also writing the files so permission issues seem unlikely.</p> <p>The files are simple text files which I'm trying to display in the browser control when the links are clicked.</p> <h1>Code setting the <code>DocumentText</code> Property of the browser control:</h1> <pre><code>Private Shared Function LoadProgramHtml(ByVal programFiles() As String) As String Dim programHtml As New StringBuilder If ProgramFiles.Length &gt; 0 Then programHtml.AppendLine("&lt;HTML&gt;") programHtml.AppendLine("&lt;HEAD&gt;&lt;/HEAD&gt;") programHtml.AppendLine("&lt;BODY bottommargin='0' leftmargin='10' rightmargin='0' topmargin='10'&gt;") programHtml.AppendLine("&lt;span&gt;Programs&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;") For Each program As String In ProgramFiles Dim progInfo As New FileInfo(program) programHtml.AppendLine(String.Format("&lt;a href='file://{0}'&gt;{1}&lt;/a&gt;&lt;br /&gt;", progInfo.FullName, progInfo.Name.ToUpper)) Next programHtml.AppendLine("&lt;/BODY&gt;") programHtml.AppendLine("&lt;/HTML&gt;") End If WebViewer.DocumentText = programHtml.ToString() End Function </code></pre> <h1>Code to handle the navigating event:</h1> <pre><code>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), "&lt;br /&gt;") End If e.Cancel = True End If End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload