前一段時間,寫個程序,要創(chuàng)建一個多級目錄。MFC中的CreateDirectory()函數(shù)只能夠創(chuàng)建一級目錄,所以我就編了個創(chuàng)建多級目錄的函數(shù)。
函數(shù)很簡單,直接調(diào)用就可以了。
函數(shù)說明:
入口:要創(chuàng)建的目錄,CString類型
出口:BOOL類型,true 成功,false 失敗。
代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | CreateMuliteCategory(CString FilePath) { CString Tempstr,dir; Tempstr= "" ; int index=Tempstr.Find( '' ); while (!SetCurrentDirectory(FilePath)) //?D?????t?Dê?·?′??ú { index ++; while ( '' != FilePath.GetAt(index)) { index ++; } CString s; s.Format( "%d" ,index); Tempstr = FilePath.Left(index); //μ?μ?2?·??·?? CreateDirectory(Tempstr,NULL); } if (!SetCurrentDirectory(FilePath)) { MessageBox( "???t?D′′?¨ê§°ü!" ); return false ; } return true ; } |
說明:函數(shù)沒有采用異常處理,可以根據(jù)需要自行修改。此函數(shù)只是提供了一個解決辦法。