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

 

 

Last modified: July 31, 2019

Comments

Write a Reply or Comment

Your email address will not be published.