vc 檔案目錄的刪除

2021-05-22 07:56:57 字數 933 閱讀 2410

專案中突然要使用刪除檔案的功能,於是看看書,網上找找資料,總結了一些方法,其實都很簡單的。希望對大家有幫助:

第一種方法:   定義乙個檔案類物件來操作

cfile   tempfile;  

tempfile.remove(指定檔名);

第二種方法:  使用系統函式 deletefile( lpcstr filename )刪除檔案    _rmdir(),刪除目錄 deletedirectory(stempdir);  刪除目錄 removedirectory(stempdir);刪除目錄

eg:  deletefile(   char   *tempfilename);  

上面提到的刪除目錄的方法只能刪除空目錄(即資料夾),如果目錄下有檔案或者子目錄,就不能刪除了,vc裡好像沒有直接的函式,只能手動寫個函式來刪除了:

【問題】怎樣刪除乙個非空目錄,及其目錄裡面所有內容:

【解答1】如果不進行遞迴刪除。你可以使用api函式shfileoperation,它可以一次刪除目錄及其下面的子目錄和檔案。

示例**:  

bool deltree(lpctstr lpszpath)

【解答2】使用遞迴呼叫,逐個刪除:

示例**:

bool deletedirectory(char *dirname)//如刪除 deletedirectory("c://aaa")

else}}

tempfind.close();

if(!removedirectory(dirname))

return true;

}//刪除資料夾目錄(非空)

bool deletedirectory(char* sdirname)

//下面是應用,cstring m_strdir 是乙個資料夾路徑,如:d:downloadpic

bool delall()

vc 刪除目錄或檔案與目錄下檔案

因為vc中沒有刪除非空目錄的函式,所以要用以下方法來做 注意 路徑字串str的最後不要加 void deletedir cstring str else 不是資料夾 deletefile strdel 刪除檔案 api finder.close removedirectory str 刪除資料夾 a...

vc 刪除目錄或檔案與目錄下檔案

因為vc中沒有刪除非空目錄的函式,所以要用以下方法來做 注意 路徑字串str的最後不要加 void deletedir cstring str else 不是資料夾 deletefile strdel 刪除檔案 api finder.close removedirectory str 刪除資料夾 a...

vc徹底刪除目錄

文章 網路 屬於vc分類 電腦程式設計網整理 2009116 常用檔案函式 if finder.isdirectory finder.isdots 如果是目錄 isdirectory 判斷是否目錄 isdots 判斷是否為.或.在dos中說明每個目錄下都有預設的兩個目錄分別為 和 分別表示上一層目錄...