How to get the root folder of an ASP.NET application

I am trying to get the root folder of an application. I used the following code, but this gives a folder bin, but I need the root folder of the application. Can this be obtained?

// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;

// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);
+3
source share
4 answers

The place where your exe is is the root of the application.

You can use string appPath = Path.GetDirectoryName(Application.ExecutablePath);to get the path to the application.

If you want to find the folder where the solution is located, I suggest starting with exe and then going to the directory tree until you get to the folder containing the .sln file. Not too sure why you would like to do this, though.

EDIT: , asp.net. ( ):

public static string MappedApplicationPath
{
   get
   {
      string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
      if(APP_PATH == "/")      //a site
         APP_PATH = "/";
      else if(!APP_PATH.EndsWith(@"/")) //a virtual
         APP_PATH += @"/";

      string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
      if(!it.EndsWith(@"\"))
         it += @"\";
      return it;
   }
}
+11

, , , , .

, , , SO ( ) #), .

+1

what is the root folder, if not the folder from which the exe file is run?
it could be anything!
you can use Directory.Parentto navigate the tree, but you need to write code for this specifically

0
source

If you are trying to change the solution folder, it may make more sense to create a script construct than a runtime application. From these scenarios, you have easy access to the solution structure.

http://visualstudiohacks.com/general/customize-your-project-build-process/

0
source

All Articles