一、獲取當前文件的路徑
string str1=Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執(zhí)行的exe的文件名。 string str2=Environment.CurrentDirectory;//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。(備注:按照定義,如果該進程在本地或網(wǎng)絡驅(qū)動器的根目錄中啟動,則此屬性的值為驅(qū)動器名稱后跟一個尾部反斜杠(如“C:\”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅(qū)動器和子目錄路徑[如“C:\mySubDirectory”])。 string str3=Directory.GetCurrentDirectory(); //獲取應用程序的當前工作目錄。 string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集。 string str5=Application.StartupPath;//獲取啟動了應用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。 string str6=Application.ExecutablePath;//獲取啟動了應用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。 string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱。
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 獲取模塊的完整路徑。 2. System.Environment.CurrentDirectory 獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄。 3. System.IO.Directory.GetCurrentDirectory() 獲取應用程序的當前工作目錄。這個不一定是程序從中啟動的目錄啊,有可能程序放在C:\www里,這個函數(shù)有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什么東東,這是任何應用程序最后一次操作過的目錄,比如你用Word打開了E:\doc\my.doc這個文件,此時執(zhí)行這個方法就返回了E:\doc了。 4. System.AppDomain.CurrentDomain.BaseDirectory 獲取程序的基目錄。 5. System.Windows.Forms.Application.StartupPath 獲取啟動了應用程序的可執(zhí)行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個"\"而已。 6. System.Windows.Forms.Application.ExecutablePath 獲取啟動了應用程序的可執(zhí)行文件的路徑及文件名,效果和1一樣。 7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase 獲取和設置包括該應用程序的目錄的名稱。
二、操作環(huán)境變量
利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系統(tǒng)環(huán)境變量,如:System.Environment.GetEnvironmentVariable("windir")就可以取得windows系統(tǒng)目錄的路徑。 以下是一些常用的環(huán)境變量取值: System.Environment.GetEnvironmentVariable("windir"); System.Environment.GetEnvironmentVariable("INCLUDE"); System.Environment.GetEnvironmentVariable("TMP"); System.Environment.GetEnvironmentVariable("TEMP"); System.Environment.GetEnvironmentVariable("Path");
三、應用實例
編寫了一個WinForm程序,項目文件存放于D:\Projects\Demo,編譯后的文件位于D:\Projects\Demo\bin\Debug,最后的結果如下:
1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe 2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug 3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug 4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\ 5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\ 6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug 7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\ System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
|