将本地 HTML 文件/代码导入 Web 浏览器

发布于 2024-12-19 17:53:44 字数 646 浏览 6 评论 0原文

我正在尝试获取本地 HTML 文件以在 VB.NET 程序的 WebBrowser 中显示。我正在使用下面的代码,但它似乎不起作用,而且我不明白为什么:

    'first method
    WebBrowser1.Navigate(@".\index.html");

    'second method
    HTML = "normal"
    WebBrowser1.Document.Body.InnerHtml = HTML

当我运行第一个方法时,它会在调试控制台中产生错误“”。如果我尝试不使用 @,我会得到一个空白页面。但是,如果我更改地址,那么我知道它是一个损坏的 URL,我会收到一条 404 消息,这使得它看起来像是找到了文件但没有渲染它?

第二种方法与第一种方法相同,只是不产生错误,就像找到文本但什么也不做一样。

在这两个示例中,我都尝试了以下 HTML 和纯文本变体:

<b>bold</b>normal

为什么

normal

这个简单的代码不起作用?

I'm trying to get a local HTML file to display within a WebBrowser in a VB.NET program. I'm using the code below, however it doesn't seem to work, and I can't figure out why:

    'first method
    WebBrowser1.Navigate(@".\index.html");

    'second method
    HTML = "normal"
    WebBrowser1.Document.Body.InnerHtml = HTML

The first method produces the error "" in the Debug console when I go to run it. If I try it with out the @, I get an empty white page. If I change the address, however ,so I know its a broken URL, I get a 404 message, which makes it seem like it's finding the file but not rendering it?

The second method does the same as the first except no error is produced, its like its finding the text but doing nothing.

In both examples I have tried the following HTML and plain text variations:

<b>bold</b>normal

and

normal

Why isn't this simple code working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦醒时光 2024-12-26 17:53:44

@ 是针对 C# 的; VB.NET 不需要它,因为 VB.NET 有一个不同的(读:更好的 :-))字符串转义系统。因此,删除字符串前面的 @,并删除行后面的 ;,这也是 C# 的。

问题是,由于您使用的是 WebBrowser,因此您需要一个 file:/// URL。您可以执行以下操作,其中最简单的可能是将您的 WebBrowser 指向 about:blank 并直接将文件放入其中,如下所示:

WebBrowser1.Document.Write(IO.File.ReadAllText("index.html"))

例如。您还可以获取文件的绝对路径,并使用它:

WebBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\index.html"))

The @ thing is for C#; you don't need it for VB.NET because VB.NET has a different (read: better :-)) escaping system for strings. So, remove the @ before the string, and also get rid of the ; after your lines, which is also C#.

The problem is that, since you're using a WebBrowser, you need a file:/// URL. There are a couple things you can do, the most simple of which is probably to point your WebBrowser to about:blank and put the file in directly, like so:

WebBrowser1.Document.Write(IO.File.ReadAllText("index.html"))

For example. You could also get the file's absolute path, and use that:

WebBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\index.html"))
征棹 2024-12-26 17:53:44

我完全同意Minitech给出的答案。我正在制作一个 HTML 代码测试器并编写了这段代码并且它起作用了。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sb As New System.Text.StringBuilder
    sb.AppendLine(RichTextBox1.Text)
    IO.File.WriteAllText("htmltester.html", sb.ToString())
    WebBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\htmltester.html"))
End Sub
End Class

这段代码适用于我的程序,我想告诉你,请删除那些“@”和“;”。

I fully agree with the answer given by Minitech. I was making an HTML code tester and wrote this code and it worked.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sb As New System.Text.StringBuilder
    sb.AppendLine(RichTextBox1.Text)
    IO.File.WriteAllText("htmltester.html", sb.ToString())
    WebBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\htmltester.html"))
End Sub
End Class

This code worked for my program and i want to tell you that please remove those '@' and ';'.

在梵高的星空下 2024-12-26 17:53:44

我发现另一个可行的选项,无需创建文件。

WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()

Another option I found that works, don't have to create a file.

WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()

信仰 2024-12-26 17:53:44

创建一个打开文件对话框并将文件名引用到将显示路径的文本框或变量...

OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
WebBrowser1.Navigate(TextBox1.Text)

然后使用 Webbrowser1.navigate 导航到变量或文本框路径..

Make an open file dialog and reference file name to a text box or variable which will display de path ...

OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
WebBrowser1.Navigate(TextBox1.Text)

then use Webbrowser1.navigate to navigate to the variable or textbox path ..

愚人国度 2024-12-26 17:53:44

简单使用:

System.diagnostics.Process.start(Path)

并将路径替换为你的路径。

Simple use:

System.diagnostics.Process.start(Path)

and replace the path with your path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文