Load Text from File to String variable using C#

In this example, we will show you how to load a text file into a string variable in C#. For this purpose, we are going to use the StreamReader.ReadToEnd method to read from a text file on your local machine, or on a network folder.

First, you will need to add the namespace below:

using System.IO;

And the main code:

string str;
using (var streamReader = new StreamReader(@"c:\test.txt", Encoding.UTF8))
str = streamReader.ReadToEnd();

Happy coding!

Related Article



Asha Shoukeir

About Asha shoukeir

Software Developer and System Administrator

View all posts by Asha shoukeir →

Leave a Reply