小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#獲取當(dāng)前應(yīng)用程序所在路徑及環(huán)境變量

 偷心無痕 2016-01-24

一、獲取當(dāng)前文件的路徑


string str1=Process.GetCurrentProcess().MainModule.FileName;//可獲得當(dāng)前執(zhí)行的exe的文件名。 
string str2=Environment.CurrentDirectory;//獲取和設(shè)置當(dāng)前目錄(即該進(jìn)程從中啟動(dòng)的目錄)的完全限定路徑。(備注:按照定義,如果該進(jìn)程在本地或網(wǎng)絡(luò)驅(qū)動(dòng)器的根目錄中啟動(dòng),則此屬性的值為驅(qū)動(dòng)器名稱后跟一個(gè)尾部反斜杠(如“C:\”)。如果該進(jìn)程在子目錄中啟動(dòng),則此屬性的值為不帶尾部反斜杠的驅(qū)動(dòng)器和子目錄路徑[如“C:\mySubDirectory”])。 
string str3=Directory.GetCurrentDirectory(); //獲取應(yīng)用程序的當(dāng)前工作目錄。 
string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測(cè)程序集。 
string str5=Application.StartupPath;//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。 
string str6=Application.ExecutablePath;//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。 
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設(shè)置包含該應(yī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í)不一定返回什么東東,這是任何應(yīng)用程序最后一次操作過的目錄,比如你用Word打開了E:\doc\my.doc這個(gè)文件,此時(shí)執(zhí)行這個(gè)方法就返回了E:\doc了。
4. System.AppDomain.CurrentDomain.BaseDirectory
   獲取程序的基目錄。
5. System.Windows.Forms.Application.StartupPath 
   獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個(gè)"\"而已。
6. System.Windows.Forms.Application.ExecutablePath 
   獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑及文件名,效果和1一樣。
7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
   獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱。


二、操作環(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");


三、應(yīng)用實(shí)例


編寫了一個(gè)WinForm程序,項(xiàng)目文件存放于D:\Projects\Demo,編譯后的文件位于D:\Projects\Demo\bin\Debug,最后的結(jié)果如下:


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\



    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多