C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Virtual paths
~/App_Data/Sample.xml
~/
~/Map.txt
Physical paths
C:\Website\Files\Sample.xml
C:\Website\Default.aspx
C:\Website\Map.txt
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.
Example code that uses MapPath: C#
using System;
using System.Web;
/// <summary>
/// This is an example code-behind file you can put in App_Code.
/// It shows examples of using MapPath in code-behind.
/// </summary>
public class Example
{
public 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");
}
}
Note: MapPath is very simple and unless you have a typo in the argument, it won't cause you any problems.
Tip: MapPath can work as a bridge between website-specific virtual paths, and a physical path that most .NET IO methods will require.
PhysicalApplicationPath