Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

write a web page with content that is read from a text file

  Asked By: Reginheraht    Date: Sep 25    Category: Asp.net    Views: 3341

I'm trying to write a web page with content that is read from a text
file. At the moment the only way i can do this is reading it in line
by line which would be fine but it means i loase any formatting or
indenting from the text. Any ideas on a way to get around this?



4 Answers Found

Answer #1    Answered By: Corbin Jones     Answered On: Sep 25

Try this function - Just stick it in a label and you are on your way.

Function readfile(ByVal contentRequest As String) As String

Trace.Warn("Requested: ", contentRequest)

Dim FILENAME As String = Server.MapPath("/pplContent/" & contentRequest &


'Dim FILENAME As String = "Rand.txt"

'Get a StreamReader class that can be used to read  the file

Dim objStreamReader As StreamReader


objStreamReader = File.OpenText(FILENAME)

Dim contents As String = objStreamReader.ReadToEnd()

lblNicerOutput.Text = contents.Replace(vbCrLf, "<br>")


lblNicerOutput.Text = "<h3>Sorry that file  can not be located</h3>"

End Try

'We may wish to replace carraige returns with <br>s


End Function

Answer #2    Answered By: Taylor Evans     Answered On: Sep 25

Try this:

StreamReader sr = new StreamReader (File.Open(Server.MapPath(filePath),
FileMode.Open) );

FileContent.Text = sr.ReadToEnd(); // FileContent is a text  area control


Answer #3    Answered By: Benjamin Simpson     Answered On: Sep 25

Are you stuck with reading  data from the file  or are you open to other

Answer #4    Answered By: Adalwen Fischer     Answered On: Sep 25

Text files generally DON'T have any formatting. Except maybe for tabs and
returns. To acomplish that you could do the string.Replace() and returns with