1. 程式人生 > >【C#學習筆記】獲取當前應用程序所在路徑及環境變量

【C#學習筆記】獲取當前應用程序所在路徑及環境變量

環境 nbsp for filename 字符 dom process ati 反斜杠

轉自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html

一、獲取當前文件的路徑

string str1=Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執行的exe的文件名。
string str2=Environment.CurrentDirectory;//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。(備註:按照定義,如果該進程在本地或網絡驅動器的根目錄中啟動,則此屬性的值為驅動器名稱後跟一個尾部反斜杠(如“C:\”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅動器和子目錄路徑[如“C:\mySubDirectory”])。

string str3=Directory.GetCurrentDirectory(); //獲取應用程序的當前工作目錄。
string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集。
string str5=Application.StartupPath;//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
string str6=Application.ExecutablePath;//獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase
;//獲取或設置包含該應用程序的目錄的名稱。

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
獲取應用程序的當前工作目錄。這個不一定是程序從中啟動的目錄啊,有可能程序放在C:\www裏,這個函數有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什麽東東,這是任何應用程序最後一次操作過的目錄,比如你用Word打開了E:\doc\my.doc這個文件,此時執行這個方法就返回了E:\doc了。

4. System.AppDomain.CurrentDomain.BaseDirectory
獲取程序的基目錄。
5. System.Windows.Forms.Application.StartupPath
獲取啟動了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字符串後面多了一個"\"而已。
6. System.Windows.Forms.Application.ExecutablePath
獲取啟動了應用程序的可執行文件的路徑及文件名,效果和1一樣。
7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
獲取和設置包括該應用程序的目錄的名稱。

二、操作環境變量

利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系統環境變量,如:System.Environment.GetEnvironmentVariable("windir")就可以取得windows系統目錄的路徑。
以下是一些常用的環境變量取值:
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\

【C#學習筆記】獲取當前應用程序所在路徑及環境變量