Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Read and display a text file

From Wiki

Jump to: navigation, search


Summary: An example of how we can read a text file and bind it's contents to a GridView control

There may be occasions when we would like to display the contents of a flat file (such as a text file). To do so, we can make use of the System.IO.StreamReader object.

First, let's create a simple text file (in this case I've named it TextFile.txt):

  1. This is line 1  
  2. This is line 2  
  3. This is line 3  
  4. This is line 4  
  5. This is line 5

Then, we'll create a page with just a GridView on it that we'll use to display the contents of this file:

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DisplayTextFile.aspx.vb" Inherits="DisplayTextFile" %>  
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>Untitled Page</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server">  
  13.         </asp:GridView>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>

Next, we need to create an instance of the StreamReader object and point it at the text file. You'll notice that I've used the Server.MapPath method to get a full path to my text file and I've used this method as I placed the file in the root directory of my application, so if you haven't done the same, you'll have to amend the path. We then need to loop through the file using the Peek method and add each line to an object that can be bound to the GridView (in this case I've used an ArrayList). We can then bind this ArrayList directly to the GridView:

  1. Imports System.IO  
  2.  
  3. Partial Class DisplayTextFile
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.         ' Declarations  
  8.         Dim objStreamReader As New StreamReader(Server.MapPath("TextFile.txt"))
  9.         Dim arrText As New ArrayList
  10.  
  11.         ' Loop through the file and add each line to the ArrayList  
  12.         Do While objStreamReader.Peek() >= 0
  13.             arrText.Add(objStreamReader.ReadLine)
  14.         Loop
  15.  
  16.         ' Close the reader  
  17.         objStreamReader.Close()
  18.  
  19.         ' Bind the results to the GridView  
  20.         GridView1.DataSource = arrText
  21.         GridView1.DataBind()
  22.     End Sub
  23. End Class

This shows the method in the simplest form I could think of, so feel free to improve on this and apply whatever formatting necessary to show the file contents in a well presented manner.


This Hack is part of the ASP.NET Hacks collection

455 Rating: 2.0/5 (2 votes cast)