GianT971
GianT971

Reputation: 4523

Access site directory path with streamreader in asp.net

from a class within the App_Code folder in ASP.NET, how can I access the path of the root directory of the website? I tried:

    StreamReader sr = new StreamReader("../Questions.aspx");

But it gave me the path in Program Files...

So how can I do this? At least I thought I could navigate from the App_Code folder to the upper folder in directory

EDIT: I am not developping a web application, but a web site

Upvotes: 4

Views: 5579

Answers (2)

Vamsi
Vamsi

Reputation: 4253

Try using

    string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
    StreamReader sr = new StreamReader(filePath + @"\Questions.aspx");

Upvotes: 5

Nasir
Nasir

Reputation: 11451

Since under "App_Code", you do not have an instance of "HttpServerUtility" object or "Server" variable, you can pass it into your function and use it to translate site paths:

App_Code > Test.cs

using System.Web;

public class Test
{
    public static string getfile(HttpServerUtility Server )
    {
        return Server.MapPath("~/Default.aspx");
    }
}

And when you call it from your ASPX pages, call it as:

string filepath = Test.getfile(Server);

Upvotes: 1

Related Questions