How do I get the root directory of my ASP.NET server application?
asp.net
Solution
Server.MapPath("~");
Will get you the root directory of the current application, as a path on the disk. E.g., `C:\inetpub\...`
Note that the `~` character can be used as part of web paths in ASP.NET controls as well, it'll fill in the URL to your application.
If your class doesn't have Server property, you can use static
HttpContext.Current.Server.MapPath("~")
Problem
In the start-up code (ie no request) of my ASP.NET application I need to get the path to the root of my app. I need this to open a file I have in a folder off the root directory. How can I get this?
Related problems
- httpcontext.current.server.mappath Object reference not set to an instance of an object
- How do you determine the physical path of a file without an HttpContext?
- Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?
- How to get the physical location of an ASP.NET web application without using HttpContext.Current?