c 獲取相對路徑

2021-09-07 23:56:17 字數 4162 閱讀 7429

一、獲取當前檔案的路徑

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獲取程式的基目錄。

5獲取和設定包括該應用程式的目錄的名稱。

6獲取啟動了應用程式的可執行檔案的路徑。效果和2、5一樣。只是5返回的字串後面多了乙個

"\"而已

7獲取啟動了應用程式的可執行檔案的路徑及檔名,效果和1一樣。

二、操作環境變數

利用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");

system.environment.systemdirectory ;c:/windows/system32目錄12

.c# winform用

d:「system.threading.thread.getdomain().basedirectory」:獲取當前應用程式所在目錄的路徑,最後包含「\」;

e:「environment.currentdirectory」:獲取當前應用程式的路徑,最後不包含「\」;

f:「system.io.directory.getcurrentdirectory」:獲取當前應用程式的路徑,最後不包含「\」;

3用「environment.currentdirectory」和「system.io.directory.getcurrentdirectory」將得到「 system32」目錄的路徑;

using

system.windows.forms」宣告該引用;

4.在解除安裝程式獲取系統安裝的目錄:

system.reflection.assembly curpath =system.reflection.assembly.getexecutingassembly();

string path=curpath.location;//

得到安裝程式類setuplibrary檔案的路徑,獲取這個檔案路徑所在的目錄即得到安裝程式的目錄;

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

c# 相對路徑 系統路徑

2007-12-22

09:53

//獲取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。

//可獲得當前執行的exe的檔名。

string str1 =process.getcurrentprocess().mainmodule.filename;

//獲取和設定當前目錄(即該程序從中啟動的目錄)的完全限定路徑。備註 按照定義,如果該程序在本地或網路驅動器的根目錄中啟動,則此屬性的值為驅動器名稱後跟乙個尾部反斜槓(如「c:\」)。如果該程序在子目錄中啟動,則此屬性的值為不帶尾部反斜槓的驅動器和子目錄路徑(如「c:\mysubdirectory」)。

string str2=environment.currentdirectory;

//獲取應用程式的當前工作目錄。

string str3=directory.getcurrentdirectory();

//獲取基目錄,它由程式集衝突解決程式用來探測程式集。

//獲取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。

//獲取啟動了應用程式的可執行檔案的路徑,包括可執行檔案的名稱。

//獲取或設定包含該應用程式的目錄的名稱。

//例子

//可以得到f:\learning\c#training\win\win\bin\debug

//注意自己補兩個\

在c#中,相對路徑是用".

"和"..

"表示,".

"代表當前目錄,"..

"代表上一級錄。

例如 假設我用vs2005在d:\my documents\visual studio

2005

\projects目錄裡建立了乙個名叫controls的專案,即在projects資料夾裡有乙個controls資料夾,controls資料夾裡有三個檔案:controls.sln controls資料夾 gulfofstlawrence資料夾。

d:\my documents\visual studio

2005

\projects\controls\controls\bin\debug是這個簡單專案能夠執行的可執行檔案controls.exe

現在我想要 d:\my documents\visual studio

2005

\projects\controls\gulfofstlawrence資料夾下的gulf_of_st._lawrence.mxd(arcgis desktop)工程檔案路徑。

那麼相對路徑應該就是

"..\..\..\gulfofstlawrence\gulf_of_st._lawrence.mxd

"即string filename = @"

..\..\..\gulfofstlawrence\gulf_of_st._lawrence.mxd";

心得:1

.用相對路徑能增加專案的可移植性。使乙個工程在移植過程中變得簡單,節省了大量布置與工程相關的檔案的時間。(如果設定的是絕對路徑)。

2.使用相對路徑也使程式**變得簡單

3. 但有一點必須注意:(只能在同乙個驅動器裡(如:都在d:裡)使用相對路徑)。

c 獲取相對路徑

一 獲取當前檔案的路徑 1.system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取模組的完整路徑,包括檔名。2.system.environment.currentdirectory 獲取和設定當前目錄 該程序從中啟動的...

c 獲取相對路徑

一 獲取當前檔案的路徑 1.system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取模組的完整路徑,包括檔名。2.system.environment.currentdirectory 獲取和設定當前目錄 該程序從中啟動的...

c 獲取相對路徑

獲取路徑2相對於路徑1的相對路徑 路徑1 路徑2 返回路徑2相對於路徑1的路徑 string strpath getrelativepath c windows system32 c windows system strpath system public static string getrela...