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.ReadToEnd 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;
string text; using (var streamReader = new StreamReader(@"c:\file.txt", Encoding.UTF8)) { text = streamReader.ReadToEnd(); }
Files and Folders Examples
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Read Text File – how to read lines from text file
- [C#] Load Text File to String – how to load text from file to string
- [C#] Get Files from Directory – how to get files from directory
- [C#] Delete All Files – how to delete files from the specified directory
- [C#] Get Application Directory – how to get application or assembly folder
- [C#] File Attributes – how to get or set file attributes
- [C#] Get File Time – how to get last modification time of a file
- [C#] Open File With Associated Application – how to launch the default application
See also
- StreamReader.ReadToEnd – MSDN – reads the stream to its end
- StreamReader – MSDN – class inherited from TextReader