How do I display text from simple .txt file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell me how to read text from a text file (.txt) and output it on
my web page. I am currently using asp.net VB. I know all about
entering/retriving data from a database but no help documents actually detail
how to handle files.

Any help would be most appreciated.
 
Hi Shodan,

To read a file you typically call File.Open and Read from the FileStream,
directly or through a StreamReader.

The easiest way is probably.

StreamReader sr = new StreamReader(filepath);
string s = sr.ReadToEnd();
sr.Close();

The text is now in s, decoded as UTF-8. Any other text encoding you need
to specify it in one of the overloaded StreamReader constructors.

With Framework 2.0 this process has been simplified with the methods

File.ReadAll(filepath)
File.ReadAllLines(filepath)
File.ReadAllBytes(filepath)

There are other people who knows more about the best ways to display the
content on a web page.
 
Back
Top