[ACCEPTED]-Read contents of a file using a relative path in a Web Application-io

Accepted answer
Score: 46

Use Server.MapPath("/path/to/file") and pass the result of that to File.ReadAllText():

String template = File.ReadAllText(Server.MapPath("~/Templates/") + filename);

0

Score: 7

You can use this code snippet as well.

using System.IO;
using System.Web.Hosting;

using (StreamReader sr = new StreamReader(VirtualPathProvider.OpenFile("~/foo.txt")))
{
    string content = sr.ReadToEnd();
}

0

More Related questions