.net 當前工做目錄VS當前執行目錄與shell

定義:       當前工做目錄——進行某項操做的目的目錄,會隨着OpenFileDialogSaveFileDialog等對象所肯定的目錄而改變。 函數

當前執行目錄——該進程從中啓動的目錄,即文件自身所在目錄。工做目錄與執行目錄能夠不一樣,例如一我的住在北京,但他的工做地點不必定在北京,可能在天津。 spa

方法: orm

一、獲取和設置當前工做目錄 對象

a)          System.IO.Directory.GetCurrentDirectory() 進程

說明:獲取應用程序的當前工做目錄的徹底限定路徑。該方法是對Win32 APIGetCurrentDirectory函數的一個封裝。 字符串

例如:'"C:\Documents and Settings\Administrator\My Documents" io

b)         System.Environment.CurrentDirectory   table

說明:獲取或設置當前工做目錄的徹底限定路徑。該屬性內部調用的就是System.IO.Directory.GetCurrentDirectory()方法,兩者是效果是等價的。 form

例如:'"C:\Documents and Settings\Administrator\My Documents" file

c)          System.IO.Path.GetFullPath(".")

說明:獲取指定路徑字符串的絕對路徑。也能夠用GetFullPath(".\"),GetFullPath(".\\")

例如:'"C:\Documents and Settings\Administrator\My Documents"

二、獲取和設置當前執行目錄

a)          System.Windows.Forms.Application.StartupPath

說明:獲取啓動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。經常使用於WinForm.

例如:'"D:\Projects\MyTest\bin"

b)         System.AppDomain.CurrentDomain.BaseDirectory

說明:獲取基目錄,它由程序集衝突解決程序用來探測程序集。返回路徑末尾帶「\

例如:'"D:\Projects\MyTest\bin\"

c)          System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

說明:獲取或設置包含該應用程序的目錄的名稱。返回路徑末尾帶「\

例如:'"D:\Projects\MyTest\bin\"

三、獲取和設置當前執行目錄,包括可執行文件名

a)          System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

說明:獲取模塊的完整路徑。包括可執行文件的名稱。

例如:'"D:\Projects\MyTest\bin\MyTest.exe"

b)         System.Windows.Forms.Application.ExecutablePath

說明:獲取啓動了應用程序的可執行文件的路徑,包括可執行文件的名稱。

例如:'"D:\Projects\MyTest\bin\MyTest.exe"

c)          System.Reflection.Assembly.GetExecutingAssembly().Location

說明:若是包含清單的已加載文件沒有被影像複製,則獲取該文件的基本代碼格式的位置。

例如:'"D:\Projects\MyTest\bin\MyTest.exe"

d)         System.Reflection.Assembly.GetExecutingAssembly().CodeBase

說明:獲取最初指定的程序集的位置,例如,在AssemblyName對象中指定的位置。

例如:'"file:///D:/Projects/MyTest/bin/MyTest.exe"

相關文章
相關標籤/搜索