利用Server.mapPath()取得網站相關路徑(HttpContext.Current.Server.MapPath,路徑,根目錄,目前應用程式,上一層,類別,vb,HttpContext.Current.Server.MapPath))

  • 55366
  • 0
  • 2012-06-01

利用Server.mapPath()取得網站相關路徑

'假設目前程式路徑:c:\Inetpub\wwwroot\myApp\subDir\subDir2\abc.aspx

'取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\
 Dim aaa As String = Server.MapPath("~\") 

'取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\
        Dim bbb As String = Server.MapPath("~/") 

'取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\
        Dim fff As String = Server.MapPath("~") 

'取得根目錄路徑,ex:c:\Inetpub\wwwroot\
        Dim ccc As String = Server.MapPath("/") 

'取得目前頁面路徑,ex:c:\Inetpub\wwwroot\myApp\subDir\subDir2\
        Dim ddd As String = Server.MapPath("./") 

'取得目前頁面上一層路徑,ex:c:\Inetpub\wwwroot\myApp\subDir\
        Dim eee As String = Server.MapPath("../") 

'取得目前頁面上上一層路徑,ex:c:\Inetpub\wwwroot\myApp\
        Dim hhh As String = Server.MapPath("../../") 

補充一下,如果想要在類別檔.vb使用(而非aspx.vb),是沒有Server.mapPath可用的,要改成這樣用:

Imports System.Web

HttpContext.Current.Server.MapPath("xxx")