ASP.NET總結(jié)C#中7種獲取當(dāng)前路徑的方法
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
-獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
-獲取和設(shè)置當(dāng)前目錄(該進(jìn)程從中啟動(dòng)的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
-獲取應(yīng)用程序的當(dāng)前工作目錄。這個(gè)不一定是程序從中啟動(dòng)的目錄啊,有可能程序放在C:\www里,這個(gè)函數(shù)有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時(shí)不一定返回什么東東,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
-獲取程序的基目錄。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
-獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱(chēng)。
6. System.Windows.Forms.Application.StartupPath
-獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個(gè)"\"而已
7. System.Windows.Forms.Application.ExecutablePath
-獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑及文件名,效果和1一樣。
//獲取模塊的完整路徑。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//獲取和設(shè)置當(dāng)前目錄(該進(jìn)程從中啟動(dòng)的目錄)的完全限定目錄
string path2 = System.Environment.CurrentDirectory;
//獲取應(yīng)用程序的當(dāng)前工作目錄
string path3 = System.IO.Directory.GetCurrentDirectory();
//獲取程序的基目錄
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱(chēng)
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑
string path6 = System.Windows.Forms.Application.StartupPath;
//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑及文件名
string path7 = System.Windows.Forms.Application.ExecutablePath;
StringBuilder str=new StringBuilder();
str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1);
str.AppendLine("System.Environment.CurrentDirectory:" + path2);
str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3);
str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4);
str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5);
str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6);
str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7);
string allPath = str.ToString();
/* 輸出結(jié)果
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE
*/
相關(guān)文章
.Net6開(kāi)發(fā)winform程序使用依賴注入
本文詳細(xì)講解了.Net6開(kāi)發(fā)winform程序使用依賴注入的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C#?Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕
這篇文章主要介紹了C#?Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
利用C#代碼實(shí)現(xiàn)圖片旋轉(zhuǎn)360度
本文介紹利用C#代碼實(shí)現(xiàn)圖片旋轉(zhuǎn)360度,具體實(shí)例代碼已附上,僅供大家參考,希望對(duì)大家有所幫助2016-11-11
Unity游戲開(kāi)發(fā)中的中介者模式的應(yīng)用與實(shí)現(xiàn)
中介者模式是Unity游戲開(kāi)發(fā)中常用的設(shè)計(jì)模式之一,用于減少對(duì)象之間的直接耦合,通過(guò)引入中介者來(lái)協(xié)調(diào)對(duì)象之間的交互。通過(guò)中介者模式,游戲?qū)ο罂梢愿屿`活地進(jìn)行交互和協(xié)同工作,降低系統(tǒng)復(fù)雜度,使代碼更加易于維護(hù)和擴(kuò)展2023-05-05

