asp.net 擷取url路徑方法

來源:互聯網
上載者:User

解決的方法主要是依靠 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;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.