In. NET, the code for getting the current path is usually used for Web conversion, WinForm programming, and Windows Mobile programming.

Source: Internet
Author: User

  1. // Web programming
  2. HttpContext. Current. Server. MapPath ("FileName ")
  3. System. Web. HttpContext. Current. Request. Path
  4. // Windows Programming
  5. System. Environment. CurrentDirectory
  6. // Mobile Programming
  7. Path. GetDirectoryName (System. Reflection. Assembly. GetExecutingAssembly (). GetName (). CodeBase );

A. Specify the path of the client element Resource
B. Specify the path of the server control resource
C. Determine the physical file path of the current website

======================================

When using resources on a website, you must specify the resource path.
For example, you can use a URL path to reference the URL of an image file on a page or a page at another location on a website.
Similarly, code in Web applications can read and write files using the physical file path of server-based files.
ASP. NET provides a method for referencing resources and determining the paths of pages or other resources in an application.

A. Specify the path of the client element Resource
------------------------------

In many cases, elements or controls on a page must reference external resources, such as files.
ASP. NET allows you to reference external resources in various ways.
The selected method depends on whether the client element or server control is used.

Elements (not server controls in the page, but client elements) are transmitted to the browser as they are.
Therefore, when referencing resources from the client element, the path should be constructed according to the standard URL rules in HTML.
You can use a fully qualified (absolute) URL path or various types of relative paths.
For example, if the page contains an img tag, you can use one of the following paths to set its src attribute:

Absolute URL path:
Read code edit code run effects copy HTML code save Code
If you reference resources in other locations (such as external websites), the absolute URL path is very useful.

Relative Path of the site root directory,
It is parsed Based on the root directory of the site (not an application.
The path in this example assumes that the Images folder exists in the root directory of the website:
Read the code and edit the code running effect. Copy the HTML code and save the code.
If your website is a http://www.contoso.com, the path resolves to the form below:
Http://www.contoso.com/Images/SampleImage.jpg
If cross-application resources (such as client script files)
If it is stored in the folder under the root directory of the website, the relative path of the root directory of the site is very useful.

The relative path parsed based on the current page path:
Read the code and edit the code running effect. Copy the HTML code and save the code.

Resolved to the relative path of the current page path peer to peer.
Read the code and edit the code running effect. Copy the HTML code and save the code.

Note:
By default, the browser uses the URL of the current page as the reference to parse the relative path.
However, you can include the HTML base element in the page to specify an alternative base path.

B. Specify the path of the server control resource
-----------------------------

In the ASP. NET Server Control that references resources,
You can use an absolute or relative path, which is the same as the client element.
If the relative path is used, it is parsed relative to the path of the page, user control, or topic containing the control.
For example, assume that the Controls folder contains a user control.
This user control contains an Image Web Server Control,
The ImageUrl attribute of the server control is set to the following path:
Images/SampleImage.jpg
When the user control is running, the path is resolved to the following form:
/Controls/Images/SampleImage.jpg
This applies regardless of the page location where the user control is hosted.

Absolute and relative path references in server controls have the following Disadvantages:

Absolute paths cannot be transplanted between applications.
If you move the absolute path to the application, the link will be interrupted.
If you move resources or pages to different folders,
It may be difficult to maintain the relative path using the client element style.

To overcome these shortcomings,
ASP. NET enables the web application root directory operator (~),
You can use this operator when specifying a path in the server control.
ASP. NET will ~ The operator is resolved to the root directory of the current application.
Can be used in combination ~ Operator and folder to specify the path based on the current root directory.

The following example
Demonstrate how to use the image server control to specify the relative path of the root directory of the image ~ OPERATOR:
Copy the aspx code and save the Code <asp: Image runat = "server" id = "image1"
Imageurl = "~ /Images/sampleimage.jpg "/> <asp: Image runat =" server "id =" image1"
Imageurl = "~ /Images/sampleimage.jpg "/>
In this example,
The image file will be directly read from the images folder under the root directory of the Web application,
No matter where the page is located.

Note:
~ The operator can only be recognized by the server control and is located in the server code.
Cannot change ~ The operator is used for client elements.

It can be used in any path-related attribute in the server control ~ Operator.

Note:
On the master page, the resource path is parsed Based on the path on the Content Page.

C. Determine the physical file path of the current website
----------------------------
In an application, you may need to determine the path of files or other resources on the server.
For example, if an application reads and writes text files programmatically,
The complete physical path of the file must be provided for the Read and Write methods.

Place the physical file path (such as C:/Website/MyApplication)
Hard coding is not a good practice in applications,
Because if you move or deploy an application, the path is changed.
However,
ASP. NET provides you with a way to obtain any physical file path in an application programmatically.
Then, you can use the base file path to create the complete path of the required resources.
The two most common ASP. NET functions used to determine the file path are:
Return the attributes of the HttpRequest object in the path information and the MapPath method.

Note:
Do not send the physical file path to the client,
Because they may be used by malicious users to obtain information about your applications.

Determine the path based on the request property
The following table lists the attributes of the HttpRequest object,
These attributes help you determine the path of resources in the application.
The following example assumes that the following URL is used to send browser requests:
Http://www.contoso.com/MyApplication/MyPages/Default.aspx
For these examples,
The term "virtual path" refers to a part of the request URL following the server identifier,
The virtual path is as follows:
/MyApplication/MyPages/Default. aspx
In addition, these examples assume that the physical path of the website root directory is in the following format:
C:/inetpub/wwwroot/MyApplication/
Finally, these examples assume that the physical path contains a folder named MyPages.

The relevant attributes and returned values are as follows:
ApplicationPath
Obtain the root directory path of the current application,
The path is requested no matter where the application is located.
In this example, the property returns the following content:
/

CurrentExecutionFilePath
Obtain the virtual path of the current request.
Different from FilePath,
The difference is that if the request has been redirected in the server code,
Then, CurrentExecutionFilePath is correct.
In this example, the property returns the following content:
/MyApplication/MyPages/Default. aspx
If it is the result of calling Transfer or Execute,
If you obtain the properties of the running code, the path will reflect the location of the Code.

FilePath
Obtain the virtual path of the current request.
In this example, the property returns the following content:
/MyApplication/MyPages/Default. aspx
Unlike CurrentExecutionFilePath, FilePath does not reflect server-side conversion.

Path
Obtain the virtual path of the current request. In this example, the property returns the following content:
/MyApplication/MyPages/default. aspx

PhysicalApplicationPath
Obtain the physical file system path of the root directory of the currently running application.
In this example, the property returns the following content:
C:/inetpub/wwwroot/

PhysicalPath
Obtain the physical file system path corresponding to the requested URL.
In this example, the property returns the following content:
C:/inetpub/wwwroot/MyApplication/MyPages/default. aspx

Use the MapPath Method
The MapPath method returns the complete physical path of the virtual path passed to the method.
For example, the following code returns the file path of the root directory of the website:
Visual Basic:
Copy the VB. NET code and save the code Dim rootPath As String = Server. MapPath ("~ ")
C #:
Copy C # code and save the code String rootPath = Server. MapPath ("~ ");

Note:
The path passed to the MapPath method must be the relative path of the application, rather than the absolute path.

  • Previous: blog essay activity for Google's tenth anniversary
  • Next: use Windows XP to simulate Windows Server 2003
  • 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.