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?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Blocked AccountRank #403 · 6.2K reputationPosted Jul 19, 2009, 7:45 PM EDT
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();