string str1 =Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執行的exe的文件名。
string str2=Environment.CurrentDirectory;//獲取和設置當前目錄(即該進(jìn)程從中啟動(dòng)的目錄)的完全限定路徑。
//備注 按照定義,如果該進(jìn)程在本地或網(wǎng)絡(luò )驅動(dòng)器的根目錄中啟動(dòng),則此屬性的值為驅動(dòng)器名稱(chēng)后跟一個(gè)尾部反斜杠(如“C:\”)。如果該進(jìn)程在子目錄中啟動(dòng),則此屬性的值為不帶尾部反斜杠的驅動(dòng)器和子目錄路徑(如“C:\mySubDirectory”)。
string str3=Directory.GetCurrentDirectory();//獲取應用程序的當前工作目錄。
string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來(lái)探測程序集。
string str5=Application.StartupPath;//獲取啟動(dòng)了應用程序的可執行文件的路徑,不包括可執行文件的名稱(chēng)。
string str6=Application.ExecutablePath;//獲取啟動(dòng)了應用程序的可執行文件的路徑,包括可執行文件的名稱(chēng)。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱(chēng)。
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
獲取和設置當前目錄(該進(jìn)程從中啟動(dòng)的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
獲取應用程序的當前工作目錄。這個(gè)不一定是程序從中啟動(dòng)的目錄啊,有可能程序放在C:\www里,這個(gè)函數有可能返回C:\Documents andSettings\ZYB\,或者C:\Program Files\Adobe\,有時(shí)不一定返回什么東東,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
獲取程序的基目錄。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
獲取和設置包括該應用程序的目錄的名稱(chēng)。
6. System.Windows.Forms.Application.StartupPath
獲取啟動(dòng)了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個(gè)"\"而已
7. System.Windows.Forms.Application.ExecutablePath
獲取啟動(dòng)了應用程序的可執行文件的路徑及文件名,效果和1一樣。
1.asp.net webform用“Request.PhysicalApplicationPath獲取站點(diǎn)所在虛擬目錄的物理路徑,最后包含“\”;
2.c# winform用
A:“Application.StartupPath”:獲取當前應用程序所在目錄的路徑,最后不包含“\”;
B:“Application.ExecutablePath ”:獲取當前應用程序文件的路徑,包含文件的名稱(chēng);
C:“AppDomain.CurrentDomain.BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
D:“System.Threading.Thread.GetDomain().BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
E:“Environment.CurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
F:“System.IO.Directory.GetCurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
3.c# windows service用“AppDomain.CurrentDomain.BaseDirectory”或“System.Threading.Thread.GetDomain().BaseDirectory”;
用“Environment.CurrentDirectory”和“System.IO.Directory.GetCurrentDirectory”將得到“ system32”目錄的路徑;
如果要使用“Application.StartupPath”或“Application.ExecutablePath ”,需要手動(dòng)添加對“System.Windows.Forms.dll ”的引用,并在程序開(kāi)頭用“using System.Windows.Forms”聲明該引用;
對于.net。有如下幾種方式取得應用程序路徑。
1、Server.MapPath
2、System.Windows.Forms.StartupPath
3、Type.Assembly.Location
方法2可以應用于控制臺應用程序,WinForm應用程序,Windows服務(wù),方法1可以應用于Web應用程序,方法3都可以應用。
但方法3是加載應用程序的路徑。如果是Web應用程序,取得的路徑是C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NETFiles目錄。所以Web項目還是使用Server.MapPath吧。否則建議使用方法2。如果自己新建類(lèi)庫??梢约尤雽ystem.Windows.Forms.StartupPath的引用后使用。