ASP .NET Read a text file by C#

Reading Text Files in an ASP.NET Web Page:



In this Post we get Example for read a text file which is store in server and display file data into web page.   Before Some time we found a question in a web site this was:

“I have searched and only found this information for console, but I was wondering if it is possible to read text from a file which store in server then tell.”

This Post gives Solution of this problem. Here we make a text file on server name “mytextfile.txt”. Now we want to display file data in my “.aspx page”,

How to read File By C#:

StreamReader stRead = new StreamReader(Server.MapPath("mytextfile.txt")));
For Reading file data we use this.

Read and Display Data from Text file in asp.net:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="1.aspx.cs" Inherits="load_textfiles.original" %>

<!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>show text file data in asp.net web page</title>
    <style type="text/css">
        .style1
        {
            color: #990000;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h1 align="center" class="style1">Show Text File Data in .ASPX Page</h1>
        <p align="center" class="style1">Saved File Data is
        <br />hello My Top blog Posts are :---</p>
   
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html> 

Read a text file in ASP .NET by C# :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace load_textfiles
{
    public partial class original : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                using (StreamReader stRead = new StreamReader(Server.MapPath("mytextfile.txt")))
                {
                    int i = 0;
                    string name = string.Empty;
                    string valueFromtoEnd = string.Empty;

                    while (!stRead.EndOfStream)
                    {
                        if (i > 2 && i <= 20)
                        {
                            valueFromtoEnd += stRead.ReadLine();
                        }
                        else
                        {
                            //name += stRead.ReadLine();
                            Label1.Text = Label1.Text + stRead.ReadLine() + "<br />";
                        }
                        i++;
                        if (i > 20)
                        {
                            break;
                        }
                    }
                    //Label1.Text = name;
                    //d1.InnerHtml = name;
                }
            }
        }
    }
}



 ASP .NET Read a text file by C#
Show File Data



Here we give the list of Examples related to gridview:

Comments

Popular posts from this blog