ASP.NET總結(jié)C#中7種獲取當(dāng)前路徑的方法
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
-獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
-獲取和設(shè)置當(dāng)前目錄(該進程從中啟動的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
-獲取應(yīng)用程序的當(dāng)前工作目錄。這個不一定是程序從中啟動的目錄啊,有可能程序放在C:\www里,這個函數(shù)有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什么東東,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
-獲取程序的基目錄。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
-獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱。
6. System.Windows.Forms.Application.StartupPath
-獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個"\"而已
7. System.Windows.Forms.Application.ExecutablePath
-獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑及文件名,效果和1一樣。
//獲取模塊的完整路徑。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//獲取和設(shè)置當(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)用程序的目錄的名稱
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑
string path6 = System.Windows.Forms.Application.StartupPath;
//獲取啟動了應(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)文章
c# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的簡單方法
下面小編就為大家?guī)硪黄猚# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù)
這篇文章主要介紹了C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
用C#的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變示例
params關(guān)鍵字以實現(xiàn)方法形參個數(shù)可變是C#語法的一大優(yōu)點,下面是用C#中的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變2014-09-09
C#動態(tài)查詢之巧用Expression組合多條件表達(dá)式的方法和步驟
在C#中,可以使用AndAlso和OrElse方法組合兩個Expression<Func<T, bool>>類型的表達(dá)式,下面通過實例代碼給大家分享C#動態(tài)查詢之巧用Expression組合多條件表達(dá)式,感興趣的朋友跟隨小編一起看看吧2024-05-05
C# Redis學(xué)習(xí)系列(二)Redis基本設(shè)置
這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第二篇, Redis基本設(shè)置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
C#實現(xiàn)數(shù)字轉(zhuǎn)換漢字的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)數(shù)字轉(zhuǎn)換漢字功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#利用WinForm實現(xiàn)查看指定目錄下所有圖片功能
Windows 窗體是用于生成 Windows 桌面應(yīng)用的 UI 框架, 它提供了一種基于 Visual Studio 中提供的可視化設(shè)計器創(chuàng)建桌面應(yīng)用的高效方法,本文介紹了C#利用WinForm實現(xiàn)可以查看指定目錄文件下所有圖片功能,需要的朋友可以參考下2024-05-05

