In this article I will explain with an example, how to read and display contents of HTML file using C# and VB.Net.
The HTML file will be read and then its contents will be displayed using the WebBrowser control in Windows Application (Windows Forms Application) using C# and VB.Net.
Concept
Finally the contents of the HTML file are loaded into the WebBrowser control.
HTML File
Following HTML file will be used to load and display HTML content in the WebBrowser control.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<font face='arial' size='3'>Hello,<br />
My name is <b style='color: red'>Mudassar Khan</b>.</font>
<hr />
<img src='https://www.aspsnippets.com/images/authors/Mudassar.png' />
</body>
</html>
Form Controls
The following Form consists of a WebBrowser control.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Reflection;
VB.Net
Imports System.IO
Imports System.Reflection
Read and display HTML file in WebBrowser control in Windows Application
Inside the Form Load event handler, first an object of the Assembly class is created and it is assigned the reference of the executing assembly.
Then the contents of the HTML file are read using a StreamReader class object using the GetManifestResourceStream function of the Assembly class.
Finally the contents of the StreamReader are read using the ReadToEnd method and are assigned to the WebBrowser control.
C#
private void Form1_Load(object sender, EventArgs e)
{
Assembly assembly = Assembly.GetExecutingAssembly();
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("WebBrowser_HTML_File_CS.HTML.htm"));
webBrowser1.DocumentText = reader.ReadToEnd();
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
Dim reader As New StreamReader(assmbly.GetManifestResourceStream("WebBrowser_HTML_File_VB.HTML.htm"))
WebBrowser1.DocumentText = reader.ReadToEnd()
End Sub
Screenshot
Downloads