對檔名及路徑的操作

2021-07-09 17:20:46 字數 1991 閱讀 6120

c# 獲取檔名及副檔名

string afirstname = afile.substring(afile.lastindexof("\\") + 1, (afile.lastindexof(".") - afile.lastindexof("\\") - 1));  //檔名

string alastname = afile.substring(afile.lastindexof(".") + 1, (afile.length - afile.lastindexof(".") - 1));   //副檔名

string strfilepaht="檔案路徑";

path.getfilenamewithoutextension(strfilepath);這個就是獲取檔名的

還有的就是用substring擷取

strfilepaht.substring(path.lastindexof("\\") + 1, path.length - 1 - path.lastindexof("\\"));

strfilepaht.substring(path.lastindexof("."), path.length - path.lastindexof("."));

或者用openfiledialog1.safefilename

這樣就能取到該檔案的所在目錄路徑

string path1 = system.io.path.getdirectoryname(openfiledialog1.filename) + @"\";

string path = path.getfilename("c:\my document\path\image.jpg");    //只獲取檔名image.jpg

/string fullpath = @"\website1\default.aspx";

string filename = system.io.path.getfilename(fullpath);//檔名  「default.aspx」

string extension = system.io.path.getextension(fullpath);//副檔名 「.aspx」

string filenamewithoutextension = system.io.path.getfilenamewithoutextension(fullpath);// 沒有副檔名的檔名 「default」

/system.io.path.getfilenam(filepath)       //返回帶副檔名的檔名 

system.io.path.getfilenamewithoutextension(filepath)     //返回不帶副檔名的檔名 

system.io.path.getdirectoryname(filepath)     //返回檔案所在目錄

///獲取當前程序的完整路徑,包含檔名(程序名)。

string str = this.gettype().assembly.location;

result: x:\***\***\***.exe (.exe檔案所在的目錄+.exe檔名)

//獲取新的 process 元件並將其與當前活動的程序關聯的主模組的完整路徑,包含檔名(程序名)。

string str = system.diagnostics.process.getcurrentprocess().mainmodule.filename;

result: x:\***\***\***.exe (.exe檔案所在的目錄+.exe檔名)

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

string str = system.environment.currentdirectory;

result: x:\***\*** (.exe檔案所在的目錄)

//獲取應用程式的當前工作目錄(不可靠)。

string str = system.io.directory.getcurrentdirectory();

result: x:\***\*** (.exe檔案所在的目錄)

MFC 檔案路徑 檔名操作

strfilename strpathname.mid strpathname.reversefind 1 若去掉.txt exe doc等字尾,共4個字元 strfilename strpathname.mid strpathname.reversefind 1,strpathname.lengt...

python 檔案路徑名,檔名,字尾名的操作

需要使用路徑名來獲取檔名,目錄名,絕對路徑等等。使用os.path 模組中的函式來操作路徑名。下面是乙個互動式例子來演示一些關鍵的特性 import os path users beazley data data.csv get the last component of the path os.p...

PHP 中獲取檔名及路徑

1.basename mnt img image01.jpg 函式 得到檔名 輸出結果為 image01.jpg.使用 basename uristring 我們可以得到乙個包含副檔名的檔名 如果不需要副檔名,也可以使用 basename uristring,extstring 過濾副檔名,僅僅返回...