Alex Rivera | Logout

Read contents of a file using a relative path in a Web Application

Asked 2009-12-02T12:44:01.327
23

How can I read the contents of a text file in my Web Application, using a relative path/URL?

The files are located in a directory at the root of my application.

I don't want to specify the full path to the file I want to access, since my test environment is not an exact mirror of my production environment.

Edit
Report

1 Answer

46

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

String template = File.ReadAllText(Server.MapPath("~/Templates/") + filename);
answered 2009-12-02T12:45:32.877

Your Answer