本章講述:C#獲取.exe文件的運行目錄的方法;
WPF中獲取.exe文件的運行路徑:System.Windows.Forms.Application.StartupPath
引用:WindowsBase 、System.Windows.Forms
1、獲取啟動(dòng)了應用程序的可執行文件的路徑,不包括可執行文件的名稱(chēng)。
string str = System.Windows.Forms.Application.StartupPath;result: X:/xxx/xxx (.exe文件所在的目錄)2、 獲取啟動(dòng)了應用程序的可執行文件的路徑,包括可執行文件的名稱(chēng)。
string str = System.Windows.Forms.Application.ExecutablePath;result: X:/xxx/xxx/xxx.exe (.exe文件所在的目錄+.exe文件名)3、 獲取和設置包含該應用程序的目錄的名稱(chēng)。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;result: X:/xxx/xxx/ (.exe文件所在的目錄+"/")注意事項:
System.Environment.CurrentDirectory的含義是獲取或設置當前工作路徑,而Application.StartupPath是獲取程序啟動(dòng)路徑,表面上看二者沒(méi)什么區別,但實(shí)際上區別大得很。
先說(shuō)前者:比如說(shuō)你程序放在桌面上啟動(dòng),但是中間你用了一個(gè)OpenFileDialog打開(kāi)了E盤(pán)名為Working的文件夾下的某一個(gè)文件,那么CurrentDirectory就變成E:\Working了,所以如果你想再獲取程序啟動(dòng)文件夾的某一個(gè)文件就沒(méi)用了,但是Application.StartupPath就不會(huì )這樣了,無(wú)論你中間打開(kāi)了哪個(gè)盤(pán)的文件,啟動(dòng)路徑都是在桌面那里,一直不會(huì )變。
聯(lián)系客服