Load Text from File to String [C#]
This example shows how to load text file to string variable.
StreamReader
To load text from file to string you can use StreamReader.ReÂadToEnd method. First create new instance of StreamReader. As a parameter in constructor you can pass string with file path or Stream instance. Default encoding is UTF-8.
[C#]using System.IO; StreamReader streamReader = new StreamReader(filePath); string text = streamReader.ReadToEnd(); streamReader.Close();
See also
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Get Files from Directory – how to get files from directory
- StreamReader.ReÂadToEnd – MSDN – reads the stream to its end
- StreamReader – MSDN – class inherited from TextReader
