Using StreamReader To Read HTML File/TextFile

Here I will explain how to read text or html file using StreamReader
In previous articles I explained write a html file using StreamWriter,now I will explain how to read text or html file using StreamReader.


we need to write the following code in aspx page

<asp:Button ID="btnread" runat="server" Text="Read" onclick="btnread_Click" />      


Now in code behind add the following namespaces
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnread_Click(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(Request.PhysicalApplicationPath + "nam_email.htm");
        while (sr.Peek() >= 0)
        {
            Response.Write(sr.ReadLine() + "<br />");
        }
        sr.Close();
    }
}                  
           

   
 


Post a Comment