C# Examples

Menu:


Load Text from File to String [C#]

This example shows how to load text file to string variable.

See also newer article: Read Text File

StreamReader

To load text from file to string you can use StreamReader.Re­adToEnd method. First create new instance of StreamReader. Pass file path or Stream as a constructor parameter and specify the text file encoding (default is UTF-8).

Don't forget to include namespace using System.IO;

[C#]
string text;
using (var streamReader = new StreamReader(@"c:\file.txt", Encoding.UTF8))
{
    text = streamReader.ReadToEnd();
}

Files and Folders Examples


See also

By Jan Slama, 2007 (updated 2015)