Go to Server. MapPath and server. mappath.
If you execute this statement from the class inherited by the Page class, you can simply use
DataBase = Server. MapPath ("data. mdb ");
Otherwise, write the full namespace: System. Web. HttpContext. Current. Server. MapPath ();
Note: The paths obtained by Server. MapPath are physical paths on the Server, that is, absolute paths.
1. Server. MapPath ("/")
Note: Obtain the location of the application root directory, such as C: \ Inetpub \ wwwroot \.
2. Server. MapPath ("./")
Note: Obtain the current directory of the page, which is equivalent to Server. MapPath ("").
3. Server. MapPath ("../")
Note: Obtain the parent directory of the page.
4. Server. MapPath ("~ /")
Note: Obtain the directory of the current application. If it is the root directory, it is the root directory. If it is a virtual directory, it is the location of the virtual directory, such as C: \ Inetpub \ wwwroot \ Example \.
HttpContext. Current is used in multiple threads, and HttpContext. Current is null.
Therefore, the System. Web. HttpContext. Current. Server. MapPath () in the method class cannot obtain the object in the thread call method.
It should be used as follows:
Public static string MapPath (string strPath)
{
If (HttpContext. Current! = Null)
{
Return HttpContext. Current. Server. MapPath (strPath );
}
Else // non-web program reference
{
StrPath = strPath. Replace ("/","\\");
If (strPath. StartsWith ("\\"))
{
// StrPath = strPath. Substring (strPath. IndexOf ('\', 1). TrimStart ('\\');
StrPath = strPath. TrimStart ('\\');
}
Return System. IO. Path. Combine (AppDomain. CurrentDomain. BaseDirectory, strPath );
}
}
Http://blog.csdn.net/lego2816/article/details/6781677
What is ServerMapPath?
Server Path
If (FileUpload1.HasFile)
{
String typepic = FileUpload1.PostedFile. ContentType; // obtain the Image Upload format
Int imgsize = FileUpload1.PostedFile. ContentLength; // obtain the image size.
// Determine the obtained format
If (typepic = "image/bmp" | typepic = "image/gif" | typepic = "image/pjpeg" | typepic = "image/x- png ")
{
If (imgsize/1024 <600) // determine if the value is greater than KB
{
String savepic = getname (FileUpload1.PostedFile. FileName );
FileInfo ff = new FileInfo (savepic );
String newp = ff. Name;
String savetu = Server. MapPath ("../HomeAd/" + newp );
/// // Server. mapPath () is to save the uploaded image to the server address ////////////////////
TxtImage1.Text = newp;
If (! File. Exists (savetu ))
{
FileUpload1.SaveAs (savetu );
ScriptManager. RegisterStartupScript (this. Page, this. GetType (), "onekey", "alert ('image uploaded successfully')", true );
... The remaining full text>
How can I switch back the parent directory to a ServerMapPath problem?
Suu_path = Server. MapPath ("../uvcx. asp") to suu_path = Server. MapPath ("./uvcx. asp ")