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#讀取excel數(shù)據(jù)的兩種方法實(shí)現(xiàn)
這篇文章主要介紹了c#讀取excel數(shù)據(jù)的兩種方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
C# System.TypeInitializationException 異常處理方案
這篇文章主要介紹了C# System.TypeInitializationException 異常處理方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
C#中使用HttpDownLoadHelper下載文件實(shí)例
這篇文章主要介紹了C#中使用HttpDownLoadHelper下載文件的方法,并實(shí)例講述了在webfrom中與在mvc中的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-10-10
c# Bitmap轉(zhuǎn)bitmapImage高效方法
本文主要介紹了c# Bitmap轉(zhuǎn)bitmapImage高效方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實(shí)現(xiàn)2013-08-08
C#使用Log4net實(shí)現(xiàn)將日志輸出到控制臺或者文本文檔
Log4net?是一個(gè)穩(wěn)定且功能豐富的日志庫,已經(jīng)存在多年并且被廣泛使用,這篇文章主要為大家介紹了如何使用Log4net實(shí)現(xiàn)將日志輸出到控制臺或者文本文檔,感興趣的可以了解下2024-03-03

