2013年1月13日 星期日

[C#]取得目前執行程式的目錄

抓取 WinForm 應用程式所在的目錄可使用下面方法,此方法會回傳應用程式設定輸出目錄完整路徑。
System.Windows.Forms.Application.StartupPath
抓取 Console 應用程式所在的目錄可使用下面方法。
System.AppDomain.CurrentDomain.BaseDirectory;
抓取 ASP.NET 網頁程式,所在的目錄可用下面方法。
Server.MapPath(".");
若是要包成 Library 呢? 以上皆適用。
System.AppDomain.CurrentDomain.BaseDirectory;
補充 :

protected void Page_Load(object sender, EventArgs e) {
        //傳回傳遞給方法之虛擬路徑的完整實體路徑
        //傳遞給 MapPath 方法的路徑必須是應用程式的相對路徑,而不是絕對路徑。
        Response.Write("Server.MapPath : " + Server.MapPath("~") + "
"); //抓取 ASP.NET 網頁程式,所在的目錄 Response.Write("Server.MapPath : " + Server.MapPath(".") + "
"); //取得 asp.net 應用程式在伺服器上虛擬應用程式根路徑 Response.Write("Request.ApplicationPath : " + Request.ApplicationPath + "
"); //取得目前要求的虛擬路徑 Response.Write("Request.CurrentExecutionFilePath : " + Request.CurrentExecutionFilePath + "
"); //取得目前要求的虛擬路徑,與 CurrentExecutionFilePath 屬性不同,FilePath 並不會反映伺服器端的傳輸。 Response.Write("Request.FilePath : " + Request.FilePath + "
"); //取得目前要求的虛擬路徑 Response.Write("Request.Path : " + Request.Path + "
"); //取得目前執行應用程式之根目錄的實體檔案系統路徑 Response.Write("Request.PhysicalApplicationPath : " + Request.PhysicalApplicationPath + "
"); //取得與要求的 URL 對應之實體檔案系統路徑 Response.Write("Request.PhysicalPath : " + Request.PhysicalPath + "
"); }
輸出結果
來原:http://www.dotblogs.com.tw/atowngit/archive/2009/08/23/10198.aspx

沒有留言: