C#獲取程序所在路徑的幾種方法

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
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 一樣

發佈了16 篇原創文章 · 獲贊 9 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章