天天看點

c# 擷取項目的根目錄

編寫程式的時候,經常需要用的項目根目錄。自己總結如下

 1、取得控制台應用程式的根目錄方法

方法1、environment.currentdirectory 取得或設定目前工作目錄的完整限定路徑(eg:“c:\program files

(x86)\microsoft visual studio

9.0\common7\ide”。)

方法2、appdomain.currentdomain.basedirectory

擷取基目錄,它由程式集沖突解決程式用來探測程式集(eg:“f:\testproject\testproject\posapp.web\”。)

 2、取得web應用程式的根目錄方法

 //方法1、httpruntime.appdomainapppath.tostring();//擷取承載在目前應用程式域中的應用程式的應用程式目錄的實體驅動器路徑。用于app_data中擷取

   //方法2、server.mappath("") 或者

server.mappath("~/");//傳回與web伺服器上的指定的虛拟路徑相對的實體檔案路徑

    //

方法3、request.applicationpath;//擷取伺服器上asp.net應用程式的虛拟應用程式根目錄

 3、取得winform應用程式的根目錄方法

 //1、environment.currentdirectory.tostring();//擷取或設定目前工作目錄的完全限定路徑

//2、application.startuppath.tostring();//擷取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱

3、directory.getcurrentdirectory();//擷取應用程式的目前工作目錄 (eg:“c:\program files

4、appdomain.currentdomain.basedirectory;//擷取基目錄,它由程式集沖突解決程式用來探測程式集(eg:“f:\testproject\testproject\posapp.web\”。)

5、appdomain.currentdomain.setupinformation.applicationbase;//擷取或設定包含該應用程式的目錄的名稱(eg:“f:\testproject\testproject\posapp.web\”。)

其中:以下兩個方法可以擷取執行檔案名稱

1、process.getcurrentprocess().mainmodule.filename;//可獲得目前執行的exe的檔案名。

2、application.executablepath;//擷取啟動了應用程式的可執行檔案的路徑,包括可執行檔案的名稱

參考:

 http://www.cnblogs.com/85538649/archive/2011/08/18/rootdirectory.html

繼續閱讀