解決的方法主要是依靠 httpcontext 對象,比如
httpcontext.current.request.physicalpath; // 獲得當前頁面的完整實體路徑.比如 f:xfu.nsqsprojectwebsitedefault.asp教程x
httpcontext.current.request.physicalapplicationpath; // 獲得當前程式啟動並執行實體路徑比如f:xfu.nsqsprojectwebsite
httpcontext.current.server.mappath(@""); 這個就是在頁面中的mappath了.一樣用法
httpruntime.appdomainapppath //這個是新發現的,很好用.
還有一個是用來處理在asp.net教程中調用dll檔案,而dll檔案如果想知道當前的web網站的工作目錄可以用
system.appdomain.currentdomain.basedirectory
簡單三種方法
習asp.net上傳檔案時,發現有三種方法可以擷取當前web程式目錄實體路徑:
方法一:
string spath = system.io.path.getdirectoryname(page.request.physicalpath)
方法二:
string spath = system.web.httpcontext.current.request.mappath("images/")
//("images/")是當前虛擬目錄下的任意目錄
方法三:
string spath = page.server.mappath("images/"); //("images/")是當前虛擬目錄下的任意目錄
另:擷取全路徑中的檔案名稱
string sfile = system.io.path.getfilename(sfile);
//擷取當前進程的完整路徑,包含檔案名稱(進程名)。
string str = this.gettype().assembly.location;
result: x:xxxxxxxxx.exe (.exe檔案所在的目錄+.exe檔案名稱)
//擷取新的 process 組件並將其與當前活動的進程關聯的主模組的完整路徑,包含檔案名稱(進程名)。
string str = system.diagnostics.process.getcurrentprocess().mainmodule.filename;