将本地HTML文件/代码导入WebBrowser [英] Importing a local HTML file/code into a WebBrowser
问题描述
我试图让一个本地HTML文件显示在VB.NET程序的 WebBrowser
中。我使用下面的代码,但它似乎并没有工作,我不明白为什么:
'第一个方法
WebBrowser1.Navigate(@。\index.html);
'第二种方法
HTML =正常
WebBrowser1.Document.Body.InnerHtml = HTML
当我运行它时,第一种方法会在调试控制台中产生错误。如果我用 @
来尝试它,我会得到一个空白页。但是,如果我更改了地址,那么我知道它的URL是错误的,我收到了404消息,这看起来好像在查找文件,但没有显示它?
第二种方法与第一种方法相同,除了没有产生错误,就像它找到文本但什么也不做。在这两个例子中,我尝试了下面的HTML以及纯文本变体:
< b>加粗< / b>正常
和
正常
code>为什么这个简单的代码不工作?
解决方案
@ 是用于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))
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?
解决方案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: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"))
这篇关于将本地HTML文件/代码导入WebBrowser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文