C#獲取當前程序運行路徑的方法集合

jopen 12年前發布 | 1K 次閱讀

//獲取當前進程的完整路徑,包含文件名(進程名)。
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取新的 Process 組件并將其與當前活動的進程關聯的主模塊的完整路徑,包含文件名(進程名)。 string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。 string str = System.Environment.CurrentDirectory; result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取當前 Thread 的當前應用程序域的基目錄,它由程序集沖突解決程序用來探測程序集。 string str = System.AppDomain.CurrentDomain.BaseDirectory; result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取和設置包含該應用程序的目錄的名稱。(推薦) string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。 string str = System.Windows.Forms.Application.StartupPath; result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。 string str = System.Windows.Forms.Application.ExecutablePath; result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取應用程序的當前工作目錄(不可靠)。 string str = System.IO.Directory.GetCurrentDirectory(); result: X:\xxx\xxx (.exe文件所在的目錄)</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!