So I have a web project, and I’m trying to get the root directory of the website using the c# method
Directory.GetCurrentDirectory(). I don’t want to be using a static path as the file locations will be changing in the future. This method is running in my imageProcess.aspx.cs file, but where I thought it would return:
C:Userstcbldocumentsvisual studio 2010ProjectsModelMonitoringModelMonitoringimageProcess.aspx.cs
I’m instead getting:
C:Program FilesCommon FilesMicrosoft SharedDevServer10.0
Can anyone explain why this is happening and what a possible solution might be? Thanks a lot.
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.
If you’re in an HTTP request, you can also call
Use this code:
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".")returns the current physical directory of the
file (e.g. aspx) being executed
Server.MapPath("..")returns the parent directory
Server.MapPath("~")returns the physical path to the root of the
Server.MapPath("/")returns the physical path to the root of the
domain name (is not necessarily the same as the root of the
Let’s say you pointed a web site application (http://www.example.com/) to
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
For example, if you call
Server.MapPathin following request:
Server.MapPath(".") returns D:WebAppsshopproducts Server.MapPath("..") returns D:WebAppsshop Server.MapPath("~") returns D:WebAppsshop Server.MapPath("/") returns C:Inetpubwwwroot Server.MapPath("/shop") returns D:WebAppsshop
If Path starts with either a forward (/) or backward slash (), the
MapPathmethod returns a path as if Path were a full, virtual path.
If Path doesn’t start with a slash, the
MapPath method returns a path relative to the directory of the request being processed.
Note: in C#, @ is the verbatim literal string operator meaning that the string should be used “as is” and not be processed for escape sequences.
Server.MapPath("") will produce this effect too.
For dot net 6 I use:
Cool thing about that is that it will be the same on asp.net and also on a console application.