2011-06-18 142 views
0

我目前正在尝试使用.NET WebBrowser来显示我正在编写的应用程序的帮助信息(本地存储在html文件中),但是我遇到了两个问题,都与超链接有关。VB.NET HTML超链接

首先,我有一个搜索功能,它以file:\\\C:\...的格式返回正确的URL,我可以将其复制并粘贴到浏览器中,然后它将在那里导航。然而,点击控件本身的链接本身并没有任何作用。

其次,HTML文件都包含其他HTML文件的相对路径。这些当然不起作用,因为我刚刚结束了file:\\\C:\help.html这给了我一个'网页不可用'。但我想不到解决HTML文件并将路径链接到链接前面的目录的方法。

编辑:只是为了澄清,在第一个问题我动态构建搜索结果页面,用户键入。 HTML中包含类似这样的几个结果(是的,它是未完成的,我只是显示你的链接部分):

<a style='font-family:verdana;color:#0645AD;font-size:20px;text-decoration:underline' href='C:\Users\User\Documents\project\bin\Debug\..\..\Help\FAQ.html'>FAQ</a><br />...This is the <b>FA</b>Q File.

现在,当我点击控制什么也没有发生该链接,这不是'给我一个'网页不可用'或带我去实际的网页。然而,保存HTML,并用Chrome,IE和Firefox打开它可以正常工作。

在第二个问题中,我为不同部分提供了不同的帮助文件,每个部分都包含相对于其他几个部分的链接。 VB将它们选为直接路径,并尝试从根目录开始,即file:\\ C:\ file.html。我能想到的唯一解决方案是解析文件并使用WebBrowser.Navigate(Path.Combine(pathToDirectory, nameOfHelpFile.html),这似乎比应该更有效率。

感谢

回答

0

的修复包括增加一些代码到WebBrowser控件的“导航”事件。

Private Sub HelpBrowser_Navigating(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles HelpBrowser.Navigating 
    If e.Url.Scheme = "about" And e.Url.AbsolutePath <> "blank" Then 
     ' The clicked URL is of the format about:<file>. 
     HelpBrowser.Navigate(HelpRootPath + "\" + e.Url.AbsolutePath) 
    End If 
End Sub 
0

我想你会需要向我们展示一些代码,看看你的问题是什么。我编写了一个简单的例子,使用WebBrowser控件,其中包含一个链接到另一个的HTML文件,其工作方式与预期一致。

相对链接与正在浏览的当前文档有关。如果你正在向浏览器写入原始HTML,那么我认为它链接了一个相对于它认为是根的可能是file:///c:/,但我不确定。另外,如果文件实际上位于驱动器的根目录下,则可能会遇到许可问题。

下面是工作的罚款,我的样本:

Option Strict On 
Option Explicit On 

Imports System.IO 

Public Class Form1 

    Private WithEvents WebB As WebBrowser 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     ''//Create our web browser 
     Me.WebB = New WebBrowser() 
     ''//Set it to fill the form 
     Me.WebB.Dock = DockStyle.Fill 
     ''//Add it to the form 
     Me.Controls.Add(Me.WebB) 

     ''//We will put our HTML files in this folder which is on the desktop 
     Dim WorkingFolder = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "HTMLTest") 
     ''//Create it if it doesn't exist 
     If Not Directory.Exists(WorkingFolder) Then Directory.CreateDirectory(WorkingFolder) 

     ''//The names of the two files that we are creating 
     Dim FirstFile = "Start.html" 
     Dim SecondFile = "End.html" 

     ''//Write HTML in the first file that has a link to the second file 
     My.Computer.FileSystem.WriteAllText(Path.Combine(WorkingFolder, FirstFile), <html><head><title>Start</title></head><body><a href=<%= SecondFile %>>Link to second file</a></body></html>.ToString(), False) 
     ''//Write HTML in the second file 
     My.Computer.FileSystem.WriteAllText(Path.Combine(WorkingFolder, SecondFile), <html><head><title>End</title></head><body>This is the second file</body></html>.ToString(), False) 

     ''//Tell the web browser to navigate to the second file 
     Me.WebB.Navigate(Path.Combine(WorkingFolder, FirstFile)) 
    End Sub 
End Class