How to get the Absolute Path of a File on Server

Waqas Anwar
15 June 2009
5703 Views

If you are trying to access a file on Server and you need Absolute Path of that file you can use Server.MapPath method that takes a virtual path as its argument and returns the corresponding physical or absolute path.

To get physical path of a file located on root of your website
string path = Server.MapPath("products.xml");
or

string path = Server.MapPath(".") + "\\products.xml";
To get physical path of a file located in a sub directory such as data of your website.

string path = Server.MapPath("data") + "\\products.xml";