C#中獲取程序路徑的幾種方法及其區(qū)別說(shuō)明
在開發(fā)的程序中使用自定義的配置文件屬于基本的操作了,但最近發(fā)現(xiàn)一個(gè)問題:如果程序是由用戶通過exe文件直接打開的,獲取同目錄下配置文件不會(huì)有問題;
但如果程序是由第三方應(yīng)用啟動(dòng)的,通過不同方式獲取的“當(dāng)前目錄”會(huì)有不同。
整理的幾種獲取當(dāng)前目錄的方式
及官方文檔中的注釋:
string str1=Process.GetCurrentProcess().MainModule.FileName;//獲取模塊的完整路徑。 string str2=Environment.CurrentDirectory;//獲取或設(shè)置當(dāng)前工作目錄的完全限定路徑。 string str3=Directory.GetCurrentDirectory();//獲取應(yīng)用程序的當(dāng)前工作目錄。 string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來(lái)探測(cè)程序集。 string str5=Application.StartupPath;//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。 string str6=Application.ExecutablePath;//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。 string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設(shè)置包含該應(yīng)用程序的目錄的名稱。 string str8=this.GetType().Assembly.Location;//獲取包含清單的已加載文件的路徑或UNC位置。
分別通過手動(dòng)啟動(dòng)和第三方調(diào)用的方法啟動(dòng)應(yīng)用程序(exe文件路徑:D:\test\程序啟動(dòng)路徑.exe),其值分別為:
手動(dòng)啟動(dòng)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序啟動(dòng)路徑.exe |
Environment.CurrentDirectory | D:\test |
Directory.GetCurrentDirectory() | D:\test |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序啟動(dòng)路徑.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序啟動(dòng)路徑.exe |
通過Win+R打開運(yùn)行,輸入程序路徑,或通過bat批處理命令運(yùn)行,效果與直接打開一致。
C#使用System.Diagnostics.Process.Start()方法啟動(dòng)
(調(diào)用方路徑:E:\test)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序啟動(dòng)路徑.exe |
Environment.CurrentDirectory | E:\test |
Directory.GetCurrentDirectory() | E:\test |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序啟動(dòng)路徑.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序啟動(dòng)路徑.exe |
通過cmd命令行啟動(dòng)
(cmd命令中直接輸入 D:\test\程序啟動(dòng)路徑.exe)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序啟動(dòng)路徑.exe |
Environment.CurrentDirectory | C:\Users\用戶名 |
Directory.GetCurrentDirectory() | C:\Users\用戶名 |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序啟動(dòng)路徑.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序啟動(dòng)路徑.exe |
通過LabVIEW中“執(zhí)行系統(tǒng)命令”(System Exec.vi)啟動(dòng)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序啟動(dòng)路徑.exe |
Environment.CurrentDirectory | C:\WINDOWS\system32 |
Directory.GetCurrentDirectory() | C:\WINDOWS\system32 |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序啟動(dòng)路徑.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序啟動(dòng)路徑.exe |
此方法在不同電腦中表現(xiàn)不一致,沒有對(duì)比是操作系統(tǒng)原因還是LabVIEW原因。
在同事電腦上,Environment.CurrentDirectory和Directory.GetCurrentDirectory()方式獲取的路徑為 C:\Users\用戶名\Desktop
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
WPF+DiffPlex實(shí)現(xiàn)文本比對(duì)工具
現(xiàn)行的文本編輯器大多都具備文本查詢的能力,但是并不能直觀的告訴用戶兩段文字的細(xì)微差異,所以對(duì)比工具在某種情況下,就起到了很便捷的效率。本文將利用DiffPlex實(shí)現(xiàn)簡(jiǎn)易的文本比對(duì)工具,需要的可以參考一下2022-11-11C#中GraphicsPath的Widen方法用法實(shí)例
這篇文章主要介紹了C#中GraphicsPath的Widen方法用法,實(shí)例分析了Widen方法的使用技巧,需要的朋友可以參考下2015-06-06c#使用Unity粒子實(shí)現(xiàn)炮塔發(fā)射系統(tǒng)
Unity自帶粒子發(fā)射器、動(dòng)畫器、渲染器各兩種,利用Unity的粒子系統(tǒng)制作一個(gè)炮塔發(fā)射系統(tǒng),了解粒子系統(tǒng),必須先了解每一個(gè)屬性都代表了什么,之后才能根據(jù)這些原理來(lái)調(diào)整出自己滿意的效果2022-04-04C#實(shí)現(xiàn)兩個(gè)時(shí)間相減的方法
這篇文章主要介紹了C#實(shí)現(xiàn)兩個(gè)時(shí)間相減的方法,實(shí)例分析了C#針對(duì)時(shí)間操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼
開發(fā)中有時(shí)會(huì)遇到要對(duì)文件進(jìn)行共享狀態(tài)的讀寫操作,代碼如下,需要的朋友可以參考下2012-06-06