locked
Reading Text Files RRS feed

  • Question

  • User-710761916 posted

    How can I display the contents of a text file on a page?

    Thanks,

     - Mike

    Thursday, July 3, 2008 8:59 PM

Answers

  • User-710761916 posted
    OK, I figured it out. Here is what I used:

    At the top have:

    <%@ Import Namespace="System.IO" %>

    And inside your <script runat="server"> have:

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

    If File.Exists(Server.MapPath("TXT\Games\" & GridView1.Rows(Request.QueryString("AppRow")).Cells(5).Text & ".txt")) Then
    Dim
    objStreamReader As New StreamReader(Server.MapPath("TXT\Games\" & GridView1.Rows(Request.QueryString("AppRow")).Cells(5).Text & ".txt"))

    Do While objStreamReader.Peek() >= 0
    arrText.Add(objStreamReader.ReadLine)
    Loop

    objStreamReader.Close()
    Else
    arrText.Add("No description")
    End If

    GridView2.DataSource = arrText
    GridView2.DataBind()
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 8:34 PM

All replies

  • User-1959498717 posted

    Label1.Text = file.readalltext(@"c:\test.txt");     // This will show all the text in a label.

    but you can also use stream.

     

    FileStream theFile = File.OpenRead(@"c:\test.txt");

    StreamReader reader = new StreamReader(theFile);

    result.Text = reader.ReadToEnd();

    Thursday, July 3, 2008 9:31 PM
  • User-710761916 posted

     EDIT: Never mind, does not work. I get the error:

    Compiler Error Message: BC30451: Name 'file' is not declared.

     And in the second method I get the error:

     Compiler Error Message: BC30451: Name 'FileStream' is not declared.

    Friday, July 4, 2008 1:59 PM
  • User-967720686 posted

    Hi,

    Try this .....

    Private Function ReadFile(ByVal path As String) As String
        If System.IO.File.Exists(path) Then
            Dim Sr As New System.IO.StreamReader(path)
            Return Sr.ReadToEnd()
        End If
        Return String.Empty
    End Function

     

    Friday, July 4, 2008 2:39 PM
  • User-1322853079 posted

    hi , 

    you need to import the namespace System.IO;

     

    i.e using System.IO;

     

     

     

     

     

     

    regards

    Suresh Kumar goudampally

     

     

     

    Friday, July 4, 2008 2:48 PM
  • User-710761916 posted

     Sorry I am pretty new at Visual Basic.

    Hi,

    Try this .....

    Private Function ReadFile(ByVal path As String) As String
        If System.IO.File.Exists(path) Then
            Dim Sr As New System.IO.StreamReader(path)
            Return Sr.ReadToEnd()
        End If
        Return String.Empty
    End Function

     Where Do I put this? I tried between <%# %> but it has Private underlined in blue and says Expression Expected.

    you need to import the namespace System.IO;

     

    i.e using System.IO;

    What do you mean? [:D]

     

    Friday, July 4, 2008 8:13 PM
  • Thursday, July 10, 2008 12:44 AM
  • User-710761916 posted

     Alright, thanks a bunch.

    Thursday, July 10, 2008 4:34 PM
  • User-710761916 posted

     Ok. All the examples I have seen can only open text files from the user's harddrive. How can I make it so it can read text files on the website?

    Thursday, July 24, 2008 7:58 PM
  • User-710761916 posted
    OK, I figured it out. Here is what I used:

    At the top have:

    <%@ Import Namespace="System.IO" %>

    And inside your <script runat="server"> have:

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

    If File.Exists(Server.MapPath("TXT\Games\" & GridView1.Rows(Request.QueryString("AppRow")).Cells(5).Text & ".txt")) Then
    Dim
    objStreamReader As New StreamReader(Server.MapPath("TXT\Games\" & GridView1.Rows(Request.QueryString("AppRow")).Cells(5).Text & ".txt"))

    Do While objStreamReader.Peek() >= 0
    arrText.Add(objStreamReader.ReadLine)
    Loop

    objStreamReader.Close()
    Else
    arrText.Add("No description")
    End If

    GridView2.DataSource = arrText
    GridView2.DataBind()
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 8:34 PM