如何在 C# 中的 WebBrowser 控件中加载本地 HTML 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11560639/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:19:10  来源:igfitidea点击:

How to load local HTML pages in WebBrowser control in C#

c#winformsbrowserc#-2.0

提问by Mayuresh

I have number of local HTML pages. I want to display these local HTML pages in Web browser control. When I add new page it should get append to previous page.

我有许多本地 HTML 页面。我想在 Web 浏览器控件中显示这些本地 HTML 页面。当我添加新页面时,它应该附加到上一页。

Here is the sample code for the setting Url

这是设置 Url 的示例代码

for( int i=0; i<=filecount; i++)
    web-browser.Url = new Uri(filepath[i]);

But during run time its showing the File Download pop up and web browser is empty.

但是在运行时它显示文件下载弹出和网络浏览器是空的。

回答by Mathlight

webrowser.Navigate(filepath[i]); 

something like that i remember... ;)

我记得这样的事情......;)

回答by Moble Joseph

You could load a single page as

您可以将单个页面加载为

FileStream source = new FileStream(filepath, FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;

or even like

甚至喜欢

string html = File.ReadAllText(filepath);
webBrowser1.DocumentText = html;

But if you have images, css or js in the relative paths, use

但是如果相对路径中有图像、css 或 js,请使用

Uri uri = new Uri(filepath);
webBrowser1.Navigate(uri);

回答by Tukansam

I tried:

我试过:

FileStream source = new FileStream(filepath, FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;

I had to add this also:

我还必须添加这个:

webBrowser1.DocumentStream.Close();