将本地 HTML 文件/代码导入 WebBrowser
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8405608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing a local HTML file/code into a WebBrowser
提问by tim.baker
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:
我试图让本地 HTML 文件显示在WebBrowser
VB.NET 程序中。我正在使用下面的代码,但它似乎不起作用,我不知道为什么:
'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?
当我去运行它时,第一种方法在调试控制台中产生错误“”。如果我在没有 的情况下尝试它@
,我会得到一个空白的白页。但是,如果我更改地址,因此我知道它的 URL 已损坏,我会收到一条 404 消息,这使它看起来像是在查找文件但未呈现文件?
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:
在这两个示例中,我都尝试了以下 HTML 和纯文本变体:
<b>bold</b>normal
and
和
normal
Why isn't this simple code working?
为什么这个简单的代码不起作用?
回答by Ry-
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#.
该@
件事是C#; 对于 VB.NET,您不需要它,因为 VB.NET 有一个不同的(阅读:更好的:-)
)字符串转义系统。因此,删除@
之前的字符串,并删除;
后面的行,这也是 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:
问题是,由于您使用的是WebBrowser
,因此您需要一个file:///
URL。有一对夫妇的事情可以做,其中最简单的可能是你点WebBrowser
到about:blank
,并把文件直接,就像这样:
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"))
回答by TheNewGenGamer
I fully agree with the answer given by Minitech. I was making an HTML code tester and wrote this code and it worked.
我完全同意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
This code worked for my program and i want to tell you that please remove those '@' and ';'.
这段代码适用于我的程序,我想告诉你,请删除那些“@”和“;”。
回答by OKLakegoer
Another option I found that works, don't have to create a file.
我发现的另一个选项是有效的,不必创建文件。
WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()
WebBrowser1.DocumentText = strHTML
WebBrowser1.Update()
回答by Wujing Klaus
Make an open file dialog and reference file name to a text box or variable which will display de path ...
制作一个打开的文件对话框并将文件名引用到一个文本框或变量,它将显示 de path ...
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
WebBrowser1.Navigate(TextBox1.Text)
then use Webbrowser1.navigate
to navigate to the variable or textbox path ..
然后使用Webbrowser1.navigate
导航到变量或文本框路径..