C#文件路徑操作詳細(xì)總結(jié)
一、獲取當(dā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.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱。
6. System.Windows.Forms.Application.StartupPath
獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個(gè)"\"而已
7. System.Windows.Forms.Application.ExecutablePath
獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑及文件名,效果和1一樣。
二、操作環(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");
最后貼出我進(jìn)行上面操作獲得的變量值,事先說明,本人是編寫了一個(gè)WinForm程序,項(xiàng)目文件存放于D:\Visual Studio Projects\MyApplication\LifeAssistant,編譯后的文件位于D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug,最后的結(jié)果如下:
1、 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe
2、System.Environment.CurrentDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
3、System.IO.Directory.GetCurrentDirectory()=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\
5、 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\
6、System.Windows.Forms.Application.StartupPath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
7、 System.Windows.Forms.Application.ExecutablePath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\jdk1.5.0\bin;C:\MySQLServer5.0\bin;C:\Program Files\Symantec\pcAnywhere\;C:\Program Files\Microsoft SQL Server\80\Tools\BINN
相關(guān)文章
C#用ComboBox控件實(shí)現(xiàn)省與市的聯(lián)動(dòng)效果的方法
這篇文章主要介紹了C#用ComboBox控件實(shí)現(xiàn)省與市的聯(lián)動(dòng)效果的方法,較為詳細(xì)的分析了ComboBox控件的使用原理與相關(guān)技巧,需要的朋友可以參考下2015-01-01C#實(shí)現(xiàn)簡單的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01詳解C#如何實(shí)現(xiàn)隱式類型轉(zhuǎn)換
Result?類型是許多編程語言中處理錯(cuò)誤的常用方式,包括?C#?的?dotNext?庫。在本文中,我們將通過例子回顧?C#?中?using?語句和隱式類型轉(zhuǎn)換的使用,感興趣的可以了解一下2023-01-01深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(下)
這篇文章主要介紹了深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程的相關(guān)知識(shí),文中講解的非常詳細(xì),幫助大家更好的學(xué)習(xí)c#網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-06-06c# 如何將RadioButton與DataTable數(shù)據(jù)進(jìn)行綁定
我接觸到的有將兩個(gè)控件的數(shù)據(jù)綁定、將控件的屬性與DataTable綁定,以下說說在將DataTable與RadioButton綁定的過程中出現(xiàn)的問題2012-11-11vs2019 實(shí)現(xiàn)C#調(diào)用c++的dll兩種方法
這篇文章主要介紹了vs2019 實(shí)現(xiàn)C#調(diào)用c++的dll兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Untiy Shader實(shí)現(xiàn)紋理貼圖滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Untiy Shader實(shí)現(xiàn)紋理貼圖滾動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03