標籤:winform style blog http color 使用
一、擷取當前檔案的路徑
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
擷取模組的完整路徑。可獲得當前執行的exe的檔案名稱。
2. System.Environment.CurrentDirectory 擷取和設定目前的目錄(該進程從中啟動的目錄)的完全限定目錄。擷取和設定目前的目錄(即該進程從中啟動的目錄)的完全限定路徑。(備忘:按照定義,如果該進程在本地或網路磁碟機的根目錄中啟動,則此屬性的值為磁碟機名稱後跟一個尾部反斜線(如“C:\”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜線的磁碟機和子目錄路徑[如“C:\mySubDirectory”])。
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返回的字串後面多了一個"\"而已。 6. System.Windows.Forms.Application.ExecutablePath 擷取啟動了應用程式的可執行檔的路徑及檔案名稱,效果和1一樣。 7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase 擷取和設定包括該應用程式的目錄的名稱。
8.this.GetType().Assembly.Location
擷取當前進程的完整路徑,包含檔案名稱。
二、作業環境變數
利用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\
四、在dll中有時需要使用主調用程式中的資源,這就要正確擷取調用程式的檔案名稱及其路徑等資訊。這需要和調用dll本身的檔案名稱和路徑區分開來!
這就牽扯到System.Reflection.Assembly程式集類使用了。
GetExecutingAssembly : 擷取包含當前執行的代碼的程式集
GetCallingAssembly : 返回調用當前正在執行的方法的方法的 System.Reflection.Assembly
來自:http://blog.csdn.net/jzxcheng/article/details/31361263