Assume we have a text file called filetext.txt copied to the project directory of our ASP.Net application. The following code snippet will help us to read the file content and assign it to a StringBuilder object using StreamReader class in System.IO namespace.
string root = Server.MapPath("~");
string Template = root +"\\filetext.txt";
StringBuilder line = new StringBuilder();
using (StreamReader rwOpenTemplate = new StreamReader(Template))
{
while (!rwOpenTemplate.EndOfStream)
{
line.Append(rwOpenTemplate.ReadToEnd());
}
}
In the above code, the Server.MapPath("~") will give us the root location
You need to include System.IO and System.Text namespace for this code to work.