c 獲取檔案路徑

2021-06-01 20:58:03 字數 3020 閱讀 6208

一、獲取當前檔案的路徑

1.  system.diagnostics.process.getcurrentprocess().mainmodule.filename

獲取模組的完整路徑,包括檔名。

2.  system.environment.currentdirectory

獲取和設定當前目錄(該程序從中啟動的目錄)的完全限定目錄。

3.  system.io.directory.getcurrentdirectory()

1 asp.net webform

2.c# winform

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

e:「environment.currentdirectory

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

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

3.c# windows service

」或「system.threading.thread.getdomain().basedirectory

」;用「environment.currentdirectory

」和「system.io.directory.getcurrentdirectory

」,需要手動新增對「system.windows.forms.dll 」的引用,並在程式開頭用「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

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

string 

string 

str1 

=process.getcurrentprocess().mainmodule.filename;

//獲取和設定當前目錄(即該程序從中啟動的目錄)的完全限定路徑。備註 

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

string 

str2=environment.currentdirectory;

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

string 

str3=directory.getcurrentdirectory();

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

string 

string 

string 

在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.獲取檔案路徑問題 首先確認 要查詢的檔案與可執行檔案是否在同一目錄下 同一目錄下 string filename test.txt fileinfo fi new fileinfo filename string path fi.fullname 不同目錄分兩種情況 相對路徑只能相對於應用程式的...

C 獲取檔案路徑

string files system.io.directory.getfiles path 返回指定目錄下的檔名 string str system.io.path.getfilenamewithoutextension path 返回不具有副檔名的指定路徑字串的檔名 3 system.io.pa...

C 獲取檔案路徑

winform獲取檔案路徑 string files system.io.directory.getfiles path 返回指定目錄下的檔名 string str system.io.path.getfilenamewithoutextension path 返回不具有副檔名的指定路徑字串的檔名 ...