How to use Server.MapPath
Usage:
1.server.mappath ("/") where the application root is located, such as C:\Inetpub\wwwroot\
2.server.mappath ("./") indicates the current directory of the page on which it is located
Note: equivalent to Server.MapPath ("") returns the physical file path of the page where Server.MapPath ("") is located
3.server.mappath (".. /") indicates the previous level of the directory
4.server.mappath ("~/") represents the directory of the current application-level program, if it is the root directory, is the root directory, if it is a virtual directory, is the location of the virtual directory
such as: C:\Inetpub\wwwroot\Example\ Note: equivalent to Server.MapPath ("~").
However, sometimes we need to get the root directory above the path, this time the swelling do?
Two solutions are presented below.
The first type is
Path to C #. GetFullPath get the parent directory implementation method
String path = new DirectoryInfo (".. /"). fullname;//the parent directory of the current application path
using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Namespace Pathtest
{
Class Program
{
static void Main (string[] args)
{
Use the AppDomain to get the execution directory of the current application set
string dir = appdomain.currentdomain.basedirectory;
string info = String.Format ("AppDomain method gets the current assembly directory: {0}", dir);
Console.WriteLine (info);
Use path to get the parent directory of the execution of the current application set
dir = Path.GetFullPath ("..");
info = String.Format ("path method gets the current assembly ancestor directory: {0}", dir); (www.jb51.net)
Console.WriteLine (info);
Use path to get the parent directory of the executing directory of the current application set
dir = Path.GetFullPath (@ "....");
info = String.Format ("path method gets the parent directory at the level of the current assembly directory: {0}", dir);
Console.WriteLine (info);
Use path to get the parent directory of the execution directory of the current application set
dir = Path.GetFullPath (@ "...");
info = String.Format ("path method gets the parent directory of the parent directory of the current assembly directory: {0}", dir);
Console.WriteLine (info);
Add the specified directory in the current assembly directory
dir = Path.GetFullPath (@ "io");
info = String.Format ("Add the specified directory in the current assembly directory: {0}", dir);
Console.WriteLine (info);
Console.read ();
}
}
}
This situation, not often effective, swollen do? We can take a more flexible approach, and here are some more flexible approaches.
Not only for the parent directory problem, but also flexible to deal with a lot of similar problems, thanks to a senior to my advice ~
The second way of thinking is this:
First get the root directory of the application,
String root = System.Web.HttpContext.Current.Server.MapPath ("~/");
Then use the array, the "\ \" As a delimiter, distinguish
String[] temp = root. Split ("\ \"). ToCharArray ());
Iterating over the resulting array
for (int i = 0; i < temp. Length-2; i++)
{
A + = Temp[i];
A + = "\";
}
For example, to obtain the upper level, the length-2, you can obtain the upper level of the directory
On the superior, continue to lose 2, and so on.
How C # Gets the top-level path to the current application