programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Website by URL, How to Play YouTube Videos in by using Jquery , Jquery Change div background on mouseover in , JQuery UI Datepicker (Calendar) etc.

Tuesday, September 30, 2014

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>show text file data in web page</title>
    <style type="text/css">
            color: #990000;
            text-decoration: underline;
    <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>

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();
                            //name += stRead.ReadLine();
                            Label1.Text = Label1.Text + stRead.ReadLine() + "<br />";
                        if (i > 20)
                    //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:

No comments:

Post a Comment