Example. You can call MapPath in any C# file in your ASP.NET website. You may want to include the System.Web namespace first, but this is not required.
Note The Server.MapPath does the same thing as the Request.MapPath method. In this example, the two versions will do the same thing.
Note 2 There may be some differences in different usage scenarios, but in those cases a more detailed guide would be helpful.
Also The two methods (Server.MapPath and Request.MapPath) are interchangeable in most ASP.NET projects.
/// This is an example code-behind file you can put in App_Code.
/// It shows examples of using MapPath in code-behind.
public class Example
// This will locate the Example.xml file in the App_Data folder.
// ... (App_Data is a good place to put data files.)
string a = HttpContext.Current.Server.MapPath("~/App_Data/Example.xml");
// This will locate the Example.txt file in the root directory.
// ... This can be used in a file in any directory in the application.
string b = HttpContext.Current.Request.MapPath("~/Example.txt");
A discussion. You can use the MapPath method to access many different paths on the server. If you are using a virtual shared host, there may be problems related to file permissions.
Note MapPath is simple and unless you have a typo in the argument, it will not cause you any problems.
Performance. MapPath performance was tested to be over 1000 times slower than a simple string append. It could be worthwhile to cache the paths with an appSettings cache.