Go to Server. MapPath and server. mappath.

Source: Internet
Author: User

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 ")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.