将本地 HTML 文件/代码导入 Web 浏览器
我正在尝试获取本地 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@
是针对 C# 的; VB.NET 不需要它,因为 VB.NET 有一个不同的(读:更好的:-)
)字符串转义系统。因此,删除字符串前面的@
,并删除行后面的;
,这也是 C# 的。问题是,由于您使用的是
WebBrowser
,因此您需要一个file:///
URL。您可以执行以下操作,其中最简单的可能是将您的 WebBrowser 指向 about:blank 并直接将文件放入其中,如下所示:例如。您还可以获取文件的绝对路径,并使用它:
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 afile:///
URL. There are a couple things you can do, the most simple of which is probably to point yourWebBrowser
toabout:blank
and put the file in directly, like so:For example. You could also get the file's absolute path, and use that:
我完全同意Minitech给出的答案。我正在制作一个 HTML 代码测试器并编写了这段代码并且它起作用了。
这段代码适用于我的程序,我想告诉你,请删除那些“@”和“;”。
I fully agree with the answer given by Minitech. I was making an HTML code tester and wrote this code and it worked.
This code worked for my program and i want to tell you that please remove those '@' and ';'.
我发现另一个可行的选项,无需创建文件。
WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()
Another option I found that works, don't have to create a file.
WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()
创建一个打开文件对话框并将文件名引用到将显示路径的文本框或变量...
然后使用 Webbrowser1.navigate 导航到变量或文本框路径..
Make an open file dialog and reference file name to a text box or variable which will display de path ...
then use
Webbrowser1.navigate
to navigate to the variable or textbox path ..简单使用:
并将路径替换为你的路径。
Simple use:
and replace the path with your path.