C 中獲取程式當前路徑的7種常用的方法

2021-12-30 00:28:48 字數 1480 閱讀 4793

c#中獲取程式當前路徑的7種最常用的方法

string str1 =process.getcurrentprocess().mainmodule.filename;//可獲得當前執行的exe的檔名。 

string str2=environment.currentdirectory;//獲取和設定當前目錄(即該程序從中啟動的目錄)的完全限定路徑。

//備註 按照定義,如果該程序在本地或網路驅動器的根目錄中啟動,則此屬性的值為驅動器名稱後跟乙個尾部反斜槓(如「c:」)。如果該程序在子目錄中啟動,則此屬性的值為不帶尾部反斜槓的驅動器和子目錄路徑(如「c:mysubdirectory」)。

string str3=directory.getcurrentdirectory();//獲取應用程式的當前工作目錄。

string str4=appdomain.currentdomain.basedirectory;//獲取基目錄,它由程式集衝突解決程式用來探測程式集。

string str5=application.startuppath;//獲取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。

string str6=application.executablepath;//獲取啟動了應用程式的可執行檔案的路徑,包括可執行檔案的名稱。

string str7=appdomain.currentdomain.setupinformation.applicationbase;//獲取或設定包含該應用程式的目錄的名稱。

1.   system.diagnostics.process.getcurrentprocess().mainmodule.filename

獲取模組的完整路徑。

2.   system.environment.currentdirectory

獲取和設定當前目錄(該程序從中啟動的目錄)的完全限定目錄。

3.   system.io.directory.getcurrentdirectory()

獲取應用程式的當前工作目錄。這個不一定是程式從中啟動的目錄啊,有可能程式放在c:www裡,這個函式有可能返回c:documents and settingsyb,或者c:program filesadobe,有時不一定返回什麼東東,我也搞不懂了。

4.  system.appdomain.currentdomain.basedirectory

獲取程式的基目錄。

5.  system.appdomain.currentdomain.setupinformation.applicationbase

獲取和設定包括該應用程式的目錄的名稱。

6.  system.windows.forms.application.startuppath

獲取啟動了應用程式的可執行檔案的路徑。效果和2、5一樣。只是5返回的字串後面多了乙個""而已

7.  system.windows.forms.application.executablepath

獲取啟動了應用程式的可執行檔案的路徑及檔名,效果和1一樣

C 中獲取程式當前路徑的7種常用的方法

string str1 process.getcurrentprocess mainmodule.filename 可獲得當前執行的exe的文件名。string str2 environment.currentdirectory 獲取和配置當前目錄 即該程序從中啟動的目錄 的完全限定路徑。備註 按照...

獲取程式當前路徑

expandfilename 返回檔案的全路徑 含驅動器 路徑 extractfileext 從檔名中抽取副檔名 extractfilename 從檔名中抽取不含路徑的檔名 extractfilepath 從檔名中抽取路徑名 extractfiledir 從檔名中抽取目錄名 extractfiled...

C 獲取當前路徑的7種方法

c 獲取當前路徑的方法如下 1.system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取模組的完整路徑。2.system.environment.currentdirectory 獲取和設定當前目錄 該程序從中啟動的目錄 ...