C#獲取當前路徑,獲取當前路徑的上一層路徑


 

C#獲取當前路徑的方法如下:

(1)string path1 = System.Environment.CurrentDirectory;
//C:\...\bin\Debug

-獲取和設置當前工作目錄(該進程從中啟動的目錄)的完全限定目錄。

(2)string path2 = System.IO.Directory.GetCurrentDirectory();
//C:\...\bin\Debug

-獲取應用程序的當前工作目錄。網上說這個方法得到的不一定是程序從中啟動的目錄,我試的和(1)的結果一樣,證明有時得到的還是程序從中啟動的目錄。

(3)string path3 = System.Windows.Forms.Application.StartupPath;
//C:\...\bin\Debug

-獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。

(4)string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//C:\...\bin\Debug\

-獲取基目錄,它由程序集沖突解決程序用來探測程序集。
(5)string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//C:\...\bin\Debug\

 -獲取或設置包含該應用程序的目錄的名稱。


(6)string path6 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//C:\...\bin\Debug\WindowsFormsApplication1.vshost.exe

-獲取模塊的完整路徑。
()7string path7 = System.Windows.Forms.Application.ExecutablePath;
//C:\...\bin\Debug\WindowsFormsApplication1.EXE

-獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。

 

當前路徑與相對路徑的合並:
string path8 = System.IO.Path.GetFullPath(System.IO.Path.Combine(path1, @"..\..\obj\x86\Debug"));
string[] array = System.IO.Directory.GetFiles(path8);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM