Home >

Hwo to open text file in ASP.NET

8. March 2011

Here is a code snippet to address a common problem to use simple text files using System.IO in ASP.NET:

        'Open a file for reading
        Dim FILENAME As String = Server.MapPath("Data.txt")

        'Get a StreamReader class
        Dim objStreamReader As System.IO.StreamReader
        objStreamReader = System.IO.File.OpenText(FILENAME)

        'Read the entire file into a string
        Dim contents As String = objStreamReader.ReadToEnd()

        'Optionally you may wish to replace carraige returns with <br>s
        TextBox1.Text = contents.Replace(vbCrLf, "<br>")

        objStreamReader.Close()

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading