在vc中創建目錄(文件夾)

最簡單的方法是執行dos命令:system("md ...")但是這種方法會彈出來dos窗口。 另一種創建目錄的方法:

MakeSureDirectoryPathExists

 

The MakeSureDirectoryPathExists function creates all the directories in the specified DirPath, beginning with the root.

 

				BOOL
MakeSureDirectoryPathExists(
PCSTR

DirPath





);

Parameters

DirPath
[in] Pointer to a null-terminated string that specifies a valid path name. If the final component of the path is a directory, not a file name, the string must end with a backslash (/) character.

Return Values

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE. To retrieve extended error information, call GetLastError.

Remarks

Each directory specified is created, if it does not already exist. If only some of the directories are created, the function will return FALSE.

All DbgHelp functions, such as this one, are single threaded. Therefore, calls from more than one thread to this function will likely result in unexpected behavior or memory corruption. To avoid this, you must synchronize all concurrent calls from more than one thread to this function.

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires DbgHelp.dll on Windows NT 4.0 and Windows Me/98/95.
Header: Declared in Dbghelp.h.
Library: Use Dbghelp.lib.

函數功能描述:該函數創建一個從根目錄開始的完整的指定路徑.

.函數原型:
 BOOL MakeSureDirectoryPathExists(PCSTR DirPath);

.參數:
 DirPath [in] : 指向一個以NULL結尾的包含正確的指定的路徑名,如果路徑名的結尾部分不是文件名而是文件夾,那麼要以'/'爲結束符.

.返回值:
 函數成功返回TRUE;
 函數失敗返回FALSE;要獲得具體錯誤信息用GetLastError();

.備註:
 每一級目錄如果不存在就創建它,如果只有一些目錄被創建了,那麼函數返回FALSE.

.示例代碼段:
 在用MakeSureDirectoryPathExists前,要在Project->Settings...->Link->/Object/library modules中加入imagehlp.lib.
 {
 BOOL bRet=MakeSureDirectoryPathExists("f://Directory1//Directory2//Directory3//");
 //創建目錄,要注意結尾"//".
 ASSERT(bRet);
 bRet=MakeSureDirectoryPathExists("f://Directory1//Directory2//Directory3");
 //創建目錄,但不創建Directory3,因爲沒有'//'結尾.
 ASSERT(bRet);
 bRet=MakeSureDirectoryPathExists("f://Directory1//Directory2//Directory3//test.txt");
 //創建目錄,但不創建文件,可以不用'//'結尾.
 ASSERT(bRet);
 }

.使用條件:
 Windows NT/2000: 要求是Windows NT 3.1或後續版本。
 Windows 95/98: 要求是 Windows 95或後續版本。
 頭文件: 在Dbghelp.h中定義.
 靜態庫: Dbghelp.lib.

posted on 2007-07-04 23:38 ffan 閱讀(3771) 評論(1)  編輯 收藏 引用 所屬分類: C/C++/VC

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章