Saturday, December 29, 2007

Server.MapPath Method

If you need to go to a file from your page, you may need to know the exact file path in order to access to it. In this case, you can use Server.MapPath method.

Server.MapPath method takes a path as a parameter and returns the physical path corresponding to the path.

For example, there is an xml file (database.xml) which stored a list of connection string is located in the C:\inetpub\wwwroot\webapplication1\database directory, and you need to get the connection string to connect to an appropriate database. The C:\inetpub\wwwroot directory is set as the web root directory.

The script uses the slash character to specify that the path returned should treated as a complete virtual path on the server and mapped from the root folder.
string path = Server.MapPath(“/webapplication1/database/database.xml”)
This script will output as the following:
C:\inetpub\wwwroot\webapplication1\ database\database.xml
If the passed parameter does not start with a slash character, it will map to its current directory, in this case will be C:\inetpub\wwwroot\webapplication1.
string path = Server.MapPath(“database/database.xml”)
This will produce the same result.

Below are some tricks on Server.MapPath method.

' the current directory
currDir = Server.MapPath(".")

' the parent directory
parentDir = Server.MapPath("..")

' the application's root directory
rootDir = Server.MapPath("/")

0 comments:

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
Sign up for PayPal and start accepting credit card payments instantly. http://www.emailcashpro.com
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template