close

抓取 WinForm 應用程式所在的目錄可使用下面方法,此方法會回傳應用程式設定輸出目錄完整路徑。

System.Windows.Forms.Application.StartupPath

 

抓取 Console 應用程式所在的目錄可使用下面方法。

System.AppDomain.CurrentDomain.BaseDirectory;

 

抓取 ASP.NET 網頁程式,所在的目錄可用下面方法。

Server.MapPath(".");

 

若是要包成 Library 呢? 以上皆適用。

System.AppDomain.CurrentDomain.BaseDirectory;

 

 

補充:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //傳回傳遞給方法之虛擬路徑的完整實體路徑
        //傳遞給 MapPath 方法的路徑必須是應用程式的相對路徑,而不是絕對路徑。
        Response.Write("Server.MapPath : " + Page.Server.MapPath("~") + "<br />");

        //抓取 ASP.NET 網頁程式,所在的目錄
        Response.Write("Server.MapPath : " + Server.MapPath(".") + "<br />");

        //取得 asp.net 應用程式在伺服器上虛擬應用程式根路徑
        Response.Write("Request.ApplicationPath : " + Request.ApplicationPath + "<br />");

        //取得目前要求的虛擬路徑
        Response.Write("Request.CurrentExecutionFilePath : " + Request.CurrentExecutionFilePath + "<br />");

        //取得目前要求的虛擬路徑,與 CurrentExecutionFilePath 屬性不同,FilePath 並不會反映伺服器端的傳輸。
        Response.Write("Request.FilePath : " + Request.FilePath + "<br />");

        //取得目前要求的虛擬路徑
        Response.Write("Request.Path : " + Request.Path + "<br />");

        //取得目前執行應用程式之根目錄的實體檔案系統路徑
        Response.Write("Request.PhysicalApplicationPath : " + Request.PhysicalApplicationPath + "<br />");

        //取得與要求的 URL 對應之實體檔案系統路徑
        Response.Write("Request.PhysicalPath : " + Request.PhysicalPath + "<br />");
    }
}

執行結果:

Server.MapPath : C:\Users\UserName\Desktop\WebSite4\

Server.MapPath : C:\Users\UserName\Desktop\WebSite4

Request.ApplicationPath : /

Request.CurrentExecutionFilePath : /Default.aspx

Request.FilePath : /Default.aspx

Request.Path : /Default.aspx

Request.PhysicalApplicationPath : C:\Users\UserName\Desktop\WebSite4\

Request.PhysicalPath : C:\Users\UserName\Desktop\WebSite4\Default.aspx

 

 

Reference:https://dotblogs.com.tw/atowngit/2009/08/23/10198

arrow
arrow
    文章標籤
    [Server.MapPath()]
    全站熱搜

    mitblog 發表在 痞客邦 留言(0) 人氣()