How to read a text file in the project
Hi all,
Real noob question. I have text files in a folder that I've added to my project...how do I read those text files?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Blocked AccountPosted Jul 19, 2009, 11:45 PM
try this
//Open a file for reading
string FILENAME = Server.MapPath("Rand.txt");
//Get a StreamReader class that can be used to read the file
StreamReader objStreamReader = default(StreamReader);
objStreamReader = File.OpenText(FILENAME);
//Now, read the entire file into a string
string contents = objStreamReader.ReadToEnd();
//Set the text of the file to a Web control
lblRawOutput.Text = contents;
//We may wish to replace carraige returns with
s
lblNicerOutput.Text = contents.Replace(Constants.vbCrLf, "
");
objStreamReader.Close();