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

May*_*esh 7 c# browser winforms c#-2.0

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

以下是设置Url的示例代码

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

但在运行时,它显示文件下载弹出和Web浏览器为空.

Mob*_*eph 21

您可以将单个页面加载为

FileStream source = new FileStream(filepath, FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;
Run Code Online (Sandbox Code Playgroud)

甚至喜欢

string html = File.ReadAllText(filepath);
webBrowser1.DocumentText = html;
Run Code Online (Sandbox Code Playgroud)

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

Uri uri = new Uri(filepath);
webBrowser1.Navigate(uri);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢Mobel.它现在正在工作.但它无法显示图像. (2认同)

Mat*_*ght 0

webrowser.Navigate(filepath[i]); 
Run Code Online (Sandbox Code Playgroud)

我记得类似的事情......;)