user517406
user517406

Reputation: 13763

How to get root folder of ASP.NET application

I am trying to get the root folder of the application. I have used the following code, but this gives the bin folder, but what I need is the root folder of the application. Is it possible to get this?

// 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);

Upvotes: 2

Views: 43521

Answers (4)

George Duckett
George Duckett

Reputation: 32448

The location where your exe is, is the root of the application.

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

If you want to find the folder the solution is in, i suggest starting at the exe location, then walking up the directory tree until you get to a folder containing a .sln file. Not too sure why you'd like to do this though.

EDIT: Just figured out you're creating an asp.net site. In which case you should be able to use below (found here):

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;
   }
}

Upvotes: 11

CharlesB
CharlesB

Reputation: 90446

Your program has no knowledge or link to its development environment, so there's no other way for it to know the solution directory other than you to tell him.

Either go to the parent-parente directory, as other have suggested, or pass it as an argument in the executable, or check this SO post (How do you get the solution directory in C#) that have neat ways to do this.

Upvotes: 1

Tim M.
Tim M.

Reputation: 54387

If you are trying to modify the solution folder, it may make more sense to create a build script than a runtime application. From those scripts, you have easy access to the solution structure.

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

Upvotes: 0

Menahem
Menahem

Reputation: 4144

what is the 'root' folder if not the folder where the exe file is executed from ?
it could be anything !
you might want to use Directory.Parent to navigate up the tree, but you have to write the code for that specifically

Upvotes: 0

Related Questions