Accessing a site directory directory using streamreader in asp.net

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

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

But he gave me a way in Program Files ...

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

EDIT: I am not developing a web application, but a website

+4
source share
2 answers

Try using

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

Since in "App_Code" you do not have an instance of the "HttpServerUtility" or "Server" object, you can pass it to 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, name it as:

 string filepath = Test.getfile(Server); 
+1
source

All Articles