I just want to read a text file into a string. Is there some simple code for this?

Platform: WinForms| Category: General IO

The following code reads a text file into a string object. It doesn’t get any simpler than this. In production code always make sure that you handle exceptions.


using System.IO;
....
// filePath should contain the complete path to a file.
StreamReader stream = new StreamReader(filePath); 
fileText = stream.ReadToEnd();
stream.Close();

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.