This sample shows how to use the StreamReader class to read an HTML text file, line by line, and display it on your page. Notice how the vbcrlfs (VB Line Feed/Carriage Return), in the contents of the file are replaced with HTML Line Breaks (
). That’s because an HTML page will not recognize a vbcrlf – but the HTML equivalent is the HTML line break, so we make the replacement.
This method has many uses, including the possibility of including an HTML file in an email.
Happy coding!”
<%@ Import Namespace="System.IO" %>
<script language="VB" Runat="server">
Dim FilePath as String
Dim objFileWriter as StreamWriter
Dim objStreamReader as StreamReader
Dim sContents as String=""
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
FilePath=Server.MapPath("/wishes.htm")
ReadFile()
end if
End Sub
Sub ReadFile()
objStreamReader = File.OpenText(FilePath)
Dim contents as String = objStreamReader.ReadToEnd()
lbltextFile.text = contents.Replace(vbcrlf, "<br>")
objStreamReader.Close()
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 4.0">
<title>Reading an HTML File</title>
<STYLE TYPE="text/css">
<!--
BODY {
font-family : Verdana;
font-size : 11pt;
}
-->
</STYLE>
</head>
<body>
<form id="form1" Runat="server">
<asp:Label id="lblTextFile" Runat="server" /><br>
<p>
<asp:placeholder ID="ph1" Visible="False" Runat="server">
<hr>
<asp:Label ID="label1" Runat="server" />
</asp:placeholder>
</form>
</body>
</html>