1. 程式人生 > >c# 中 獲取應用程式的路徑

c# 中 獲取應用程式的路徑

 

     示例:新建了一個windows窗體應用程式WindowsFormsApplication4,儲存在F:\Visual Studio 2008\Projects,啟動程式在F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug中

一、獲得應用程式的可執行檔案的路徑

1. System.Windows.Forms.Application.StartupPath

    public static string StartupPath { get; }

    獲取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。

   本例的結果為F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug

2. System.Windows.Forms.Application.ExecutablePath

    public static string ExecutablePath { get; }

    獲取啟動了應用程式的可執行檔案的路徑,包括可執行檔案的名稱。

     本例的結果為:F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug\ WindowsFormsApplication4.exe

二、獲取應用程式的當前工作目錄

3.   System.IO.Directory.GetCurrentDirectory()

      public static string GetCurrentDirectory ()

      獲取應用程式的當前工作目錄。這個不一定是應用程式的可執行檔案所在的目錄。未進行任何操作前,預設狀態下應用程式的當前工作目錄為應用程式的可執行檔案所在的目錄。若通過屬性vironment.CurrentDirectory改變了當前目錄,那麼此方法返回通過屬性vironment.CurrentDirectory設定的當前目錄。若在該應用程式中通過開啟對話方塊選擇了某一目錄,那麼該方法返回開啟對話方塊選擇的目錄。即該方法返回該應用程式最後一次操作過的目錄。

4.   System.Environment.CurrentDirectory

      public static string CurrentDirectory { get; set; }

      獲取和設定應用程式的當前工作目錄。說明同3.

     注意:若在程式中使用相對路徑,那麼預設儲存在應用程式的當前工作目錄下。例如:當前工作目錄為:c:\temp 在程式中使用了相對路徑“image.bmp”,那麼此影象的位置為:c:\temp\image.bmp中