StreamReader class in VB.NET

Here, we see how to read data from an external source.

StreamReader class

StreamReader class is used to read data from a file. In this Blog we read some data from a text file in the web page. we create a object of StreamReader class. Read method is used to read character from input stream.

Now suppose we want to read 100 character from a file.

Example:

.ASPX code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="countchar.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div style="height: 171px">

        <strong>

        Character:</strong>&nbsp;&nbsp;&nbsp;&nbsp;

       <div id="div1" runat ="server" >

            </div>

        <br />

        <br />

       

    </div>

    </form>

</body>

</html>

.VB code

Imports System.IO

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim path As String = "c:\temp\MyTest.txt"

        Dim sr As New StreamReader(path)

        If True Then

            Dim m As Integer = 1

            For i As Integer = 1 To 99

                div1.InnerHtml += CChar(ChrW(sr.Read()))

                m = m + 1

            Next

        End If

    End Sub

End Class