Use this code:
HttpContext.Current.Server.MapPath("~")
Detailed description:
Server.MapPath specifies a relative or virtual path to map to the physical directory.
Server.MapPath(".") Returns the current physical directory of the file (for example, aspx)Server.MapPath("..") returns the parent directoryServer.MapPath("~") returns the physical path to the root of the applicationServer.MapPath("/") returns the physical path to the root of the domain name (not necessarily the same as the application root)
Example:
Say you pointed the website app ( http://www.example.com/ ) to
C:\Inetpub\wwwroot
and installed your storeโs application (sub-web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in the following query:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then
Server.MapPath(".") returns D:\WebApps\shop\products Server.MapPath("..") returns D:\WebApps\shop Server.MapPath("~") returns D:\WebApps\shop Server.MapPath("/") returns C:\Inetpub\wwwroot Server.MapPath("/shop") returns D:\WebApps\shop
If the Path begins with a forward (/) or backslash (), the MapPath method returns the path as if the Path was a full, virtual path.
If the path does not start with a slash, the MapPath method returns the path relative to the directory of the request being processed.
Note: in C #, @ is a string string operator, meaning that the string should be used "as is" and not processed for escape sequences.
Footnote
Server.MapPath(null) and Server.MapPath("") will also produce this effect.
Harshal Doshi Jain Dec 16 '14 at 8:51 2014-12-16 08:51
source share