方法1、Environment.CurrentDirectory 取得或設置當前工作目錄的完整限定路徑
方法2、AppDomain.CurrentDomain.BaseDirectory 獲取基目錄,它由程序集沖突解決程序用來探測程序集
1、Environment.CurrentDirectory.ToString();//獲取或設置當前工作目錄的完全限定路徑
2、Application.StartupPath.ToString();//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱
3、Directory.GetCurrentDirectory();//獲取應用程序的當前工作目錄
4、AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集
5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱
HttpContext.Current
返回當前請求的 HttpContext 對象。如此我們就可以直接訪問Request、Response、Session、Application等對象,和Page中訪問等同。
我們無需再將Page用參數的方式傳遞到我們的類庫對象中。
string name = HttpContext.Current.Request.Param["name"];
HttpContext.Current.Response.Write("豬八戒好吃懶做!");
獲取網站根目錄的方法有幾種如:
Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..")
Page.Request.ApplicationPath
運行結果:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub
以上的方法可以在.aspx中訪問,但是如果你在。cs文件就不能用。
HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
在.cs文件中可以用。
但是HttpContext.Current.Server.MapPath();這個獲取的是文件的路徑而不是根目錄。
只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個才是獲取的根目錄,在寫獲取數據庫路徑是應該用這個,其他的都有問題。